Mybatis简单入门
引入mybatis
1 2 3 4 5
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency>
|
首先准备Bean
1 2 3 4 5 6 7 8 9 10 11
| @Data @AllArgsConstructor @NoArgsConstructor @ToString public class Employees {
private Long employeeId; private String firstName; private String lastName; private String email; }
|
然后写配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <environments default="mysql">
<environment id="mysql"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/myemployees"/> <property name="username" value="root"/> <property name="password" value="0000"/> </dataSource> </environment> </environments>
</configuration>
|
在resources/mapper文件夹下新建一个`IEmployeeDao.xml,里面写上会使用到的SQL语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?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.runsstudio.mylibrary.dao.IEmployeeDao"> <select id="findById" resultType="com.runsstudio.mylibrary.bean.Employees"> select * from employees where employee_id=#{id} </select> <select id="findAll" resultType="com.runsstudio.mylibrary.bean.Employees"> select * from employees </select> <insert id="insert" useGeneratedKeys="true" keyProperty="employeeId"> insert into employees(first_name,last_name,email) values(#{firstName},#{lastName},#{email}) </insert>
</mapper>
|
接下来 再准备DAO接口,里面的方法和XML 的方法对应的上。注意 这里要标注@Mapper注解,如果不标注,需要在应用上标注包扫描@MapperScan(“com.runsstudio.mylibrary.dao”)
Mapper多的情况下 一般是用包扫描
1 2 3 4 5 6 7
| @Mapper public interface IEmployeeDao { public List<Employees> findAll(); public Employees findById(Long id);
public void insert(Employees e); }
|
这个接口编写完成之后,不需要编写所谓的实现类,也就是说 不需要写
public class EmployeeDaoImpl implements IemployeeDao
编写测试类,注入一个Mapper,然后运行即可
1 2 3 4 5 6 7 8
| @Autowired IEmployeeDao iEmployeeDao;
@Test void testEmployeeDao(){ List<Employees> res= iEmployeeDao.findAll(); System.out.println(res); }
|
补充: 配置resultMap
尽管约定了JAVA属性和数据库字段一一对应的方法,但是还是会出现对应不上的情况,例如表里字段有50多个,这时候可以通过resultMap手动对应字段
1 2 3 4 5 6 7 8
| <select id="getPaymentById" resultType="Payment" resultMap="BaseResultMap"> select * from payment where id=#{id} </select>
<resultMap id="BaseResultMap" type="com.runsstudio.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"></id> <id column="serial" property="serial" jdbcType="VARCHAR"></id> </resultMap>
|
补充:配置type-aliases-package
在application.yml里可以配置type-aliases-package
1 2 3 4
| mybatis:
mapperLocations: classpath:mapper/*.xml type-aliases-package: com.runsstudio.springcloud.entities
|
这样就可以在mapper里不用写全类名。
原来:
1 2 3
| <select id="getPaymentById" resultType="com.runsstudio.springcloud.Payment" resultMap="BaseResultMap"> select * from payment where id=#{id} </select>
|
现在:
1 2 3
| <select id="getPaymentById" resultType="Payment" resultMap="BaseResultMap"> select * from payment where id=#{id} </select>
|
Mybatis-plus 入门 及其对SQL的简化
安装Mybatis-plus 可以简化常用SQL的编写
首先pom引入
1 2 3 4 5
| <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency>
|
然后按照编写Mapper,让他继承BaseMapper 这里面的T就是泛型
1 2 3
| @Mapper public interface EmployeesMapper extends BaseMapper<Employees> { }
|
有了Mapper,编写一个服务EmployeeService,让他继承IService接口,同样传入泛型,里面一句代码没写
1 2
| public interface EmployeeService extends IService<Employees> { }
|
然后实现这个接口,接口里面一句代码都没写
1 2 3
| @Service public class EmployeeServiceImpl extends ServiceImpl<EmployeesMapper, Employees> implements EmployeeService { }
|
这里需要继承ServiceImpl<EmployeesMapper, Employees>
这个也是人家写好的,这样我们就不用重写接口的方法
然后我们就不需要写额外的IEmployeeDao.xml配置每条SQL,直接就可以运行很多方法
1 2 3 4 5 6 7 8 9 10
| @Autowired EmployeesMapper employeesMapper;; @Test void testEmployeeMapper(){ Employees e=new Employees(12L,"aaa","aaavv","acccc"); int res= employeesMapper.insert(e); Employees employees = employeesMapper.selectById(202); System.out.println(res); System.out.println(employees); }
|
Mybatis-plus需要注意的三点
- 表名默认使用
bean
的小写,如果需要额外指定,需要在bean上加@Table_Name
注解。
- Bean中如果有部分字段是无用的,可以加上@TableField(exist=false)
selectById
、deleteById
需要使用id
作为字段,如果字段对应不上,例如employees
表的索引字段是employee_id
,则可以用@TableId
标注
例如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Data @AllArgsConstructor @NoArgsConstructor @ToString @TableName("employee_tbl") public class Employees { @TableId private Long employeeId;
private String firstName; private String lastName; private String email;
@TableField(exist = false) private String dddd; }
|