SpringBoot2.x入门:使用MyBatis (3)

添加Mapper和DAO类:

// club.throwable.ch8.repository.mapper.CustomerMapper public interface CustomerMapper { } // club.throwable.ch8.repository.CustomerDao public interface CustomerDao extends CustomerMapper { Customer queryByName(@Param("customerName") String customerName); }

添加XML文件resource/mappings/base/BaseCustomerMapper.xml和resource/mappings/base/ExtCustomerMapper.xml:

// BaseCustomerMapper.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="club.throwable.ch8.repository.mapper.CustomerMapper"> <resultMap type="club.throwable.ch8.entity.Customer"> <id column="id" jdbcType="BIGINT" property="id"/> <result column="customer_name" jdbcType="VARCHAR" property="customerName"/> <result column="age" jdbcType="INTEGER" property="age"/> <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/> <result column="edit_time" jdbcType="TIMESTAMP" property="editTime"/> </resultMap> </mapper> // ExtCustomerMapper.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="club.throwable.ch8.repository.CustomerDao"> <resultMap type="club.throwable.ch8.entity.Customer" extends="club.throwable.ch8.repository.mapper.CustomerMapper.BaseResultMap"> </resultMap> <select resultMap="BaseResultMap"> SELECT * FROM customer WHERE customer_name = #{customerName} </select> </mapper>

细心的伙伴会发现,DAO和Mapper类是继承关系,而ext和base下对应的Mapper文件中的BaseResultMap也是继承关系

配置文件中增加h2数据源的配置:

// application.properties spring.datasource.url=jdbc:h2:mem:db_customer;MODE=MYSQL;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=FALSE spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=org.h2.Driver spring.h2.console.enabled=true spring.h2.console.path=http://www.likecs.com/h2-console spring.h2.console.settings.web-allow-others=true spring.datasource.schema=classpath:schema.sql spring.datasource.data=classpath:data.sql

添加一个启动类进行验证:

public class Ch8Application implements CommandLineRunner { @Autowired private CustomerDao customerDao; @Autowired private ObjectMapper objectMapper; public static void main(String[] args) { SpringApplication.run(Ch8Application.class, args); } @Override public void run(String... args) throws Exception { Customer customer = customerDao.queryByName("doge"); log.info("Query [name=doge],result:{}", objectMapper.writeValueAsString(customer)); customer = customerDao.queryByName("throwable"); log.info("Query [name=throwable],result:{}", objectMapper.writeValueAsString(customer)); } }

执行结果如下:

SpringBoot2.x入门:使用MyBatis

使用Mybatis生成器生成Mapper文件

有些时候为了提高开发效率,更倾向于使用生成器去预生成一些已经具备简单CRUD方法的Mapper文件,这个时候可以使用mybatis-generator-core。编写本文的时候(2020-07-18)mybatis-generator-core的最新版本为1.4.0,mybatis-generator-core可以通过编程式使用或者Maven插件形式使用。

这里仅仅简单演示一下Maven插件形式下使用mybatis-generator-core的方式,关于mybatis-generator-core后面会有一篇数万字的文章详细介绍此生成器的使用方式和配置项的细节。在项目的resources目录下添加一个generatorConfig.xml:

<?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> <context targetRuntime="MyBatis3"> <property value="true"/> <property value="UTF-8"/> <property value="`"/> <property value="`"/> <commentGenerator> <property value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property value="true"/> <property value="true"/> </commentGenerator> <jdbcConnection driverClass="org.h2.Driver" connectionURL="jdbc:h2:mem:db_customer;MODE=MYSQL;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=FALSE" userId="root" password="123456"/> <javaTypeResolver> <property value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置(实体类)--> <javaModelGenerator targetPackage="club.throwable.ch8.entity" targetProject="src/main/java"> <property value="true"/> <property value="false"/> </javaModelGenerator> <!-- 生成映射XML文件的包名和位置--> <sqlMapGenerator targetPackage="mappings.base" targetProject="src/main/resources"> <property value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="club.throwable.ch8.repository.mapper" targetProject="src/main/java"> <property value="true"/> </javaClientGenerator> <table tableName="customer" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"> </table> </context> </generatorConfiguration>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygzjp.html