美文网首页
整合数据库和mybaits

整合数据库和mybaits

作者: landscape_f117 | 来源:发表于2020-04-21 17:54 被阅读0次

    导入Mybaits相关依赖

    <!--Mybaits & Mysql-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <!--Mybaits & Mysql-->
    
    

    修改配置文件

    https://blog.csdn.net/iku5200/article/details/82856621

    mysql JDBC URL格式如下:

    jdbc:mysql://[host:port],[host:port].../[database][?参数名1][=参数值1][&参数名2][=参数值2]...

    因为之前配置了h2数据库这里配置second-datasource.//最后测试的时候又改回去了把h2的url删了https://blog.csdn.net/tiantang_1986/article/details/95190788

    server:
      port: 8080
    
    spring:
      second-datasource:
        username: root
        password: 1234
        url: jdbc:mysql://localhost:3306/test_schema?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
        driver-class-name: com.mysql.jdbc.Driver
    
    mybatis:
      mapper-locations: classpath:mapping/*Mapper.xml
      type-aliases-package: com.example.entity
    
    

    创建实体类

    建表:

    CREATE TABLE `user` (
      `id` int(32) NOT NULL AUTO_INCREMENT,
      `userName` varchar(32) NOT NULL,
      `passWord` varchar(50) NOT NULL,
      `realName` varchar(32) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    
    

    D:\springbootdemo\src\main\java\cn\greenleaf\entity\User.java

    package cn.greenleaf.entity;
    
    /**
     * @Auther: Leon Liu
     * @Date: 2020/04/11/18:21
     * @Description:
     **/
    
    public class User {
        private Integer id;
        private String userName;
        private String passWord;
        private String realName;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassWord() {
            return passWord;
        }
    
        public void setPassWord(String passWord) {
            this.passWord = passWord;
        }
    
        public String getRealName() {
            return realName;
        }
    
        public void setRealName(String realName) {
            this.realName = realName;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", userName='" + userName + '\'' +
                    ", passWord='" + passWord + '\'' +
                    ", realName='" + realName + '\'' +
                    '}';
        }
    }
    
    

    D:\springbootdemo\src\main\java\cn\greenleaf\controller\UserController.java

    package cn.greenleaf.controller;
    
    import cn.greenleaf.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @Auther: Leon Liu
     * @Date: 2020/04/11/18:27
     * @Description:
     **/
    
    @RestController
    @RequestMapping("/testBoot")
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        @RequestMapping("getUser/{id}")
        public String GetUser(@PathVariable int id) {
            return userService.Sel(id).toString();
        }
    
    }
    
    

    D:\springbootdemo\src\main\java\cn\greenleaf\service\UserService.java

    package cn.greenleaf.service;
    
    import cn.greenleaf.entity.User;
    import cn.greenleaf.mapper.UserMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    /**
     * @Auther: Leon Liu
     * @Date: 2020/04/11/18:28
     * @Description:
     **/
    
    @Service
    public class UserService {
    
        @Autowired
        UserMapper userMapper;
    
        public User Sel(int id) {
            return userMapper.Sel(id);
    
        }
    }
    
    

    D:\springbootdemo\src\main\java\cn\greenleaf\mapper\UserMapper.java

    package cn.greenleaf.mapper;
    
    import cn.greenleaf.entity.User;
    import org.springframework.stereotype.Repository;
    
    /**
     * @Auther: Leon Liu
     * @Date: 2020/04/11/18:29
     * @Description:
     **/
    @Repository
    public interface UserMapper {
    
        User Sel(int id);
    
    }
    
    

    D:\springbootdemo\src\main\resources\mapping\UserMapper.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="cn.greenleaf.mapper.UserMapper">
    
        <resultMap id="BaseResultMap" type="cn.greenleaf.entity.User">
            <result column="id" jdbcType="INTEGER" property="id" />
            <result column="userName" jdbcType="VARCHAR" property="userName" />
            <result column="passWord" jdbcType="VARCHAR" property="passWord" />
            <result column="realName" jdbcType="VARCHAR" property="realName" />
        </resultMap>
    
        <select id="Sel" resultType="cn.greenleaf.entity.User">
            select * from user where id = #{id}
        </select>
    
    </mapper>
    
    

    测试

    浏览器输入http://localhost:8080/testBoot/getUser/1

    返回:

    User{id=1, userName='leo', passWord='123', realName=''}

    相关文章

      网友评论

          本文标题:整合数据库和mybaits

          本文链接:https://www.haomeiwen.com/subject/rnepihtx.html