1. 创建工程
首先,创建一个mybatis的工程:
2. 依赖
引入相关依赖:
<dependencies>
<!--springboot整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
</dependencies>
3. 创建Bean
创建一个person类:
package cn.tx.sboot.model;
import lombok.Data;
import java.util.Date;
@Data
public class Person {
private int pid;
private String pname;
private String addr;
private int gender;
private Date birth;
}
4. 创建mapper文件
- 创建一个mapper接口:
package cn.tx.sboot.mapper;
import cn.tx.sboot.model.Person;
import java.util.List;
public interface PersonMapper {
public Person selectById(int pid);
public List<Person> selectAll();
public void insert(Person p);
public void delete(int pid);
}
关于接口扫描,有两种方式:一种是在dao接口 加上@Mapper 或者在主启动类上 加@MapperScan(“cn.tx.sboot.mapper”),推荐选用@mapperscan的方式。
- 创建mapper映射文件:
<?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="cn.tx.sboot.mapper.PersonMapper">
<select id="selectById" resultType="Person">
select * from person t where t.pid = #{pid}
</select>
<select id="selectAll" resultType="Person">
select * from person
</select>
<insert id="insert" parameterType="person">
<selectKey keyProperty="pid" resultType="int" order="BEFORE">
select last_insert_id()
</selectKey>
insert into person(pid,pname,addr,gender,birth)values(#{pid},#{pname},#{addr},#{gender},#{birth})
</insert>
<delete id="delete" parameterType="int" >
delete from person where pid = #{pid}
</delete>
</mapper>
5. 创建配置文件
创建一个application.yml配置文件;
spring:
datasource:
url: jdbc:mysql://localhost:3306/personuseUnicode=true&useJDBCCompliantTimezoneShift=true&uselegacyDatetimeCode=false&serverTimezone=UTC
dtiver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxwait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedstatements: true
filters: stat,wall,1og4j
maxPoolPreparedstatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql-true;druid.stat.slowSqlMillis=500
mybatis:
mapper-locations: classpath:mapper/*.xml #设置扫描所有的mapper映射文件
configuration:
map-underscore-to-camel-case: true #设置驼峰模式和下划线模式的映射
type-aliases-package: cn.tx.sboot.model #设置别名
创建一个DataSource配置类:
package cn.tx.sboot.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DatasourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource dataSource(){ return new DruidDataSource();}
}
6. 创建测试
基本配置都写好了,就可以创建类进行测试。需要下面几个类:
- testcontroller
package cn.tx.sboot.controller;
import cn.tx.sboot.mapper.PersonMapper;
import cn.tx.sboot.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private PersonMapper personMapper;
@RequestMapping("selectById")
public Person selectById(){
return personMapper.selectById(1);
}
}
- 创建启动类:
package cn.tx.sboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("cn.tx.sboot.mapper")
public class FirstSpringApplication {
public static void main(String[] args){
SpringApplication.run(FirstSpringApplication.class,args);
}
}
- 测试:
成功。