Spring Boot(四) Mybatis-MySql

Spring Boot(四) Mybatis-MySql

0. prepare a database table


-- ----------------------------
-- Table structure for person
-- ----------------------------
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `age` int(11) DEFAULT NULL,
  `gender` char(255) DEFAULT NULL,
  `remark` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of person
-- ----------------------------
INSERT INTO `person` VALUES ('1', '法师', '25', '男', null);
INSERT INTO `person` VALUES ('2', '锤子', '28', '男', null);
INSERT INTO `person` VALUES ('3', '土豆', '18', '女', null);
INSERT INTO `person` VALUES ('4', '甜心', '19', '女', null);

Add a .pom.xml file maven dependent

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
        <!--集成druid,使用连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.0</version>
        </dependency>
    

Note: Druid is a database connection pool to achieve Alibaba open-source platform, which combines the advantages of C3P0, DBCP, PROXOOL such as DB pool, while adding log monitoring, can be a good monitor implementation DB connection pool and SQL, you can He said to be born for monitoring DB connection pool, is said to be "the best pool of connections."

II. Modify Profileapplication.properties

server.port=9997
# 数据库连接配置
spring.datasource.name=spring-boot-mybatis-mysql
spring.datasource.url=jdbc:mysql://47.105.66.132:3307/demo
spring.datasource.username=root
spring.datasource.password=xingying
# mybatis 配置
mybatis.mapper-locations=classpath:mapping/*.xml
mybatis.type-aliases-package=spring.boot.mybatis.mysql.model
# 使用druid数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Three .MyBatis Configuration

  1. 2 new directory in the resources list below: generator and mapping, mybatis for storing configuration and mapping files

  2. In the packet spring.boot.mybatis.mysqlnew package model and the mapper, for storing the generated code mybatis

  3. In the generator directory, create a file generatorConfig.xml, code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry  location="D:\Software\apache-maven-3.6.0-bin\repository\mysql\mysql-connector-java\8.0.17\mysql-connector-java-8.0.17.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/demo" userId="root" password="xingying">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="spring.boot.mybatis.mysql.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="spring.boot.mybatis.mysql.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="person" domainObjectName="Person" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>修改pom添加mybatis的maven插件
  <!-- mybatis generator 自动生成代码插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>                                          ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
  1. After opening the bag and introducing added View-> Tool Windows-> Maven Projects can be seen as widget

  1. Double-click to run, run after the project directory will automatically generate the following files:

  1. Open PersonMapper.xmlis Mybatis mapping file, the contents of which is actually Sql.

    Note: Use this plug when the generated map file may have multiple ResultMap, leading to the start time will complain

    Result Maps collection already contains value for...

    This is because there may be multiple database schema in a table have the same name. This time the simplest way is to open the file to delete redundant configuration.

    turn onPersonMapper

//自动生成的没有下面这个注解,如果在依赖注入的时候报错,记得加上这个注解
//@Component
public interface PersonMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(Person record);

    int insertSelective(Person record);

    Person selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Person record);

    int updateByPrimaryKey(Person record);
}
  1. Add our service and controller
/*
Service 接口
 */
public interface IPerson {
    int deleteByPrimaryKey(Integer id);

    int insert(Person record);

    int insertSelective(Person record);

    Person selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Person record);

    int updateByPrimaryKey(Person record);
}

/*
Person Service
 */
@Service
public class PersonImpl implements IPerson {

    /*
    注入 personMapper
     */
    @Autowired
    private PersonMapper personMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return personMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Person record) {
        return personMapper.insert(record);
    }

    @Override
    public int insertSelective(Person record) {
        return personMapper.insertSelective(record);
    }

    @Override
    public Person selectByPrimaryKey(Integer id) {
        return personMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Person record) {
        return personMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Person record) {
        return personMapper.updateByPrimaryKey(record);
    }
}

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    public IPerson person;

    @RequestMapping(method = RequestMethod.GET)
    public Person get(@RequestParam Integer id) {
        return person.selectByPrimaryKey(id);
    }
}

Note: If the red personMapper given below, because no configuration dependent injection PersonMapper scanning annotations, may be increased over the class @Componentannotation. Of course, you can also modify the idea of error, reduce the level of detection Autowired, the Severity level change warning or other negligible levels from the previous error.

  1. Start class add a comment@MapperScan("spring.boot.mybatis.mysql.mapper")

  2. Test Run

Fourth, more elegant way

    通过上面的过程,可以发现又出现了一系列的配置文件,如果我们想更优雅一点的编码,Mybatis为此也提供了,不需要配置文件的方式。注意,是更优雅一些不是更好一些,至于哪种方式更好要结合实际。

只需要在PersonMapper接口的方法上添加相应的SQL注解即可

package spring.boot.mybatis.mysql.annotate.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import spring.boot.mybatis.mysql.annotate.model.Person;

@Component
public interface PersonMapper {

    @Delete("DELETE FROM person WHERE id =#{id}")
    int deleteByPrimaryKey(Integer id);

    @Insert("INSERT INTO person(name,age,gender,remark) VALUES(#{name}, #{age}, #{gender},#{remark})")
    int insert(Person record);

    @Select("SELECT * FROM person WHERE id = #{id}")
    @Results({
            @Result(property = "id",  column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "remark", column = "remark")
    })
    Person selectByPrimaryKey(Integer id);

    @Update("UPDATE person SET name=#{name},age=#{age} WHERE id =#{id}")
    int updateByPrimaryKey(Person record);
}
  • @Select 是查询类的注解,所有的查询均使用这个
  • @Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。
  • @Insert 插入数据库使用,直接传入实体类会自动解析属性到对应的值
  • @Update 负责修改,也可以直接传入对象
  • @delete 负责删除

想要了解更多特性,可以参考Mybatis官网

五、踩坑记

  1. Service中注入personMapper报错

    PersonMapper类上加注解@Component

  2. 启动的时候报错

    Result Maps collection already contains value for...

    这是因为数据库中可能有多个schema中都有同样名字的一张表。这个时候简单的办法就是打开这个文件删除掉多余的配置即可。

示例代码

Guess you like

Origin www.cnblogs.com/hunternet/p/11645782.html