1、相关依赖
< dependency>
< ! -- jdbc-- >
< groupId> org. springframework. boot< / groupId>
< artifactId> spring- boot- starter- jdbc< / artifactId>
< / dependency>
< ! -- mybatis-- >
< dependency>
< groupId> org. mybatis. spring. boot< / groupId>
< artifactId> mybatis- spring- boot- starter< / artifactId>
< version> 2.1 .1 < / version>
< / dependency>
< ! -- mysql-- >
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql- connector- java< / artifactId>
< version> 8.0 .11 < / version>
< / dependency>
2、数据库连接(application.yml)
spring:
datasource:
username: root
password: admin
url: jdbc: mysql: / / localhost: 3306 / abc? serverTimezone= UTC& useUnicode= true & characterEncoding= utf8& useSSL= false
driver- class - name: com. mysql. cj. jdbc. Driver
一、使用注解
1、配置类添加mybatis-config.xml, mapper文件路径:
mybatis:
config- location: classpath: mybatis/ mybatis- config. xml
mapper- locations: classpath: mybatis/ mapper
2、创建一个Mapper接口类
package com. example. mapper;
import org. apache. ibatis. annotations. Delete;
import org. apache. ibatis. annotations. Insert;
import org. apache. ibatis. annotations. Options;
import org. apache. ibatis. annotations. Select;
import org. apache. ibatis. annotations. Update;
import com. example. bean. Department;
public interface DepartmentMapper {
@Select ( "select * from department where id=#{id}" )
public Department get ( Integer id) ;
@Delete ( "delete from department where id=#{id}" )
public Integer delete ( Integer id) ;
@Options ( useGeneratedKeys = true , keyProperty = "id" )
@Insert ( "insert into department(departmentName) values(#{departmentName})" )
public Integer insert ( Department department) ;
@Update ( "update department set departmentName=#{departmentName} where id=#{id}" )
public Integer update ( Department department) ;
}
2、在主程序上添加注解让spring扫描这个包下所有mapper类
@MapperScan(value=“com.example.mapper”)
package com. example;
import org. mybatis. spring. annotation. MapperScan;
import org. springframework. boot. SpringApplication;
import org. springframework. boot. autoconfigure. SpringBootApplication;
@MapperScan ( value= "com.example.mapper" )
@SpringBootApplication
public class MybatisApplication {
public static void main ( String[ ] args) {
SpringApplication. run ( MybatisApplication. class , args) ;
}
}
3、调用并使用
@Autowired
DepartmentMapper mapper;
@ResponseBody
@GetMapping ( "/get/{id}" )
public Department BB ( @PathVariable ( "id" ) int id) {
return mapper. get ( id) ;
}
二、使用配置文件
1、创建一个Mapper接口类
package com. example. mapper;
import com. example. bean. Employee;
public interface EmployeeMapper {
public Employee get ( int id) ;
public int add ( Employee employee) ;
}
2、创建mybatis的配置文件和实体类的SQL配置文件
mapper文件夹放置所有实体类mapper配置文件
mybatis文件夹放置mybatis的配置文件
mybatis-config.xml
< ? 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>
< environments default = "development" >
< environment id= "development" >
< transactionManager type= "JDBC" / >
< dataSource type= "POOLED" >
< property name= "driver" value= "${driver}" / >
< property name= "url" value= "${url}" / >
< property name= "username" value= "${username}" / >
< property name= "password" value= "${password}" / >
< / dataSource>
< / environment>
< / environments>
< mappers>
< mapper resource= "org/mybatis/example/BlogMapper.xml" / >
< / mappers>
< / configuration>
所以可以直接这样
< ? 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>
< / configuration>
EmployeeMapper.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= "com.example.mapper.EmployeeMapper" >
< select id= "get" resultType= "com.example.bean.Employee" >
select * from employee where id = #{ id}
< / select>
< insert id= "add" >
insert into employee ( lastName, email, gender, d_id) values ( #{ lastName} , #{ email} , ) #{ gender} , #{ d_id} )
< / insert>
< / mapper>
3、调用并使用
@Autowired
EmployeeMapper employeeMapper;
@ResponseBody
@GetMapping ( "/emp/{id}" )
public Employee getEmp ( @PathVariable ( "id" ) int id) {
return employeeMapper. get ( id) ;
}
当然注解和配置文件两种方式可以一起使用