之前一直用jpa,正好要看mybatis源码,先学习下怎么用。
Mybatis 初期使用比较麻烦,需要各种配置文件、实体类、Dao 层映射关联、还有一大推其它配置。当然 Mybatis 也发现了这种弊端,初期开发了generator可以根据表结果自动生产实体类、配置文件和 Dao 层代码,可以减轻一部分开发量;后期也进行了大量的优化可以使用注解了,自动管理 Dao 层和配置文件等。
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> <type>pom</type> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.9-rc</version> <type>pom</type> </dependency> 复制代码
Spring Boot 会自动加载 spring.datasource.* 相关配置,数据源就会自动注入到 sqlSessionFactory 中,sqlSessionFactory 会自动注入到 Mapper 中
######################################## jdbc_config ######################################## spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my_test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull spring.datasource.username=root spring.datasource.password=root ######################################## Hikari数据源 ######################################## spring.datasource.type=com.zaxxer.hikari.HikariDataSource # 最小空闲连接数量 spring.datasource.hikari.minimum-idle=5 # 最大连接数 默认10 spring.datasource.hikari.maximum-pool-size=15 # 自动提交 spring.datasource.hikari.auto-commit=true # 空闲连接存活最大时间 默认600000ms spring.datasource.hikari.idle-timeout=30000 # 连接池母子 spring.datasource.hikari.pool-name=DatebookHikariCP # 连接池中连接的最长声明周期 0为永久 默认1800000ms spring.datasource.hikari.max-lifetime=1800000 # 连接超时时间 默认30000ms spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.connection-test-query=SELECT 1 ######################################## mybatis配置 ######################################## # xml路径, 可以在入口文件处使用注解@MapperScan # @MapperScan("com.example.demo.mapper") mybatis.mapper-locations=classpath:**/mapper/*.xml # model路径 mybatis.type-aliases-package=com.example.demo.model 复制代码
@Data public class User { private Integer Id; private String userName; private String password; private String phone; } 复制代码
mapper是存放sql的地方,可以使用注解,也可以用xml,下面介绍几种常用注解:
@Mapper public interface UserMapper { @Insert("insert into t_user (id, user_name, password, phone) values (#{id}, #{userName}, #{password}, #{phone})") void insert(User user); @Select("select * from t_user") @Results({ @Result(property = "userName", column = "user_name") }) List<User> users(); } 复制代码
<?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="com.example.demo.dao.UserDao" > <sql id="BASE_TABLE"> t_user </sql> <sql id="BASE_COLUMN"> userId,userName,password,phone </sql> <insert id="insert" parameterType="com.example.demo.model.User"> INSERT INTO <include refid="BASE_TABLE"/> <trim prefix="(" suffix=")" suffixOverrides=","> userName,password, <if test="phone != null"> phone, </if> </trim> <trim prefix="VALUES(" suffix=")" suffixOverrides=","> #{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR}, <if test="phone != null"> #{phone, jdbcType=VARCHAR}, </if> </trim> </insert> <select id="users" resultType="com.example.demo.model.User"> SELECT <include refid="BASE_COLUMN"/> FROM <include refid="BASE_TABLE"/> </select> </mapper> 复制代码
@RunWith(SpringRunner.class) @SpringBootTest public class UserMapperTest { @Autowired UserMapper userMapper; @Test public void testInsert() { User user1 = new User(); user1.setId(1); user1.setUserName("daye1"); user1.setPassword("mima1"); user1.setPhone("123"); User user2 = new User(); user2.setUserName("daye2"); user2.setPassword("mima2"); user2.setPhone("456"); userMapper.insert(user1); userMapper.insert(user2); } @Test public void testUsers() { System.out.println(userMapper.users()); } } 复制代码
jpa | mybatis |
---|---|
entity | model |
repository | mapper |
service | service |