⦁ 访问数据库
⦁ 整合MyBatis框架
⦁ 引入关于Mybatis的starter
⦁ 在用户类使用Mybatis别名
package com.springboot.mybatis.pojo;
import org.apache.ibatis.type.Alias;
import com.springboot.mybatis.enumeration.SexEnum;
@Alias(value = "user") // Mybatis 指定别名
public class User {
private Long id = null;
private String userName = null;
private String note = null;
// 性别枚举,这里需要使用typeHandler进行转换
private SexEnum sex = null;
public User() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public SexEnum getSex() {
return sex;
}
public void setSex(SexEnum sex) {
this.sex = sex;
}
}
⦁ 性别枚举类
package com.springboot.mybatis.enumeration;
public enum SexEnum {
MALE(1, "男"), FEMALE(2, "女");
private int id;
private String name;
public static SexEnum getEnumById(int id) {
for(SexEnum sex:SexEnum.values()) {
if(sex.getId()==id) {
return sex;
}
}
return null;
}
private SexEnum(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
⦁ 性别typeHandler
package com.springboot.mybatis.typehandler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import com.springboot.mybatis.enumeration.SexEnum;
//声明JdbcType 为整型
@MappedJdbcTypes(JdbcType.INTEGER)
// 声明JavaType为SexEnum
@MappedTypes(value = SexEnum.class)
public class SexTypeHandler extends BaseTypeHandler
// 通过列明读取性别
@Override
public SexEnum getNullableResult(ResultSet rs, String col) throws SQLException {
int sex = rs.getInt(col);
if (sex != 1 && sex != 2) {
return null;
}
return SexEnum.getEnumById(sex);
}
// 通过下标读取性别
@Override
public SexEnum getNullableResult(ResultSet rs, int index) throws SQLException {
int sex = rs.getInt(index);
if (sex != 1 && sex != 2) {
return null;
}
return SexEnum.getEnumById(sex);
}
// 通过存储过程读取性别
@Override
public SexEnum getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
return null;
}
// 设置非空性别参数
@Override
public void setNonNullParameter(PreparedStatement ps, int index, SexEnum sex, JdbcType jdbcType)
throws SQLException {
ps.setInt(index, sex.getId());
}
}
⦁ 用户映射文件(userMapper.xml)
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
⦁ 定义MyBatis操作接口
package com.springboot.mybatis.dao;
import org.springframework.stereotype.Repository;
import com.springboot.mybatis.pojo.User;
@Repository
public interface MybatisUserDao {
public User getUser(Long id);
}
⦁ 服务层接口及实现类
package com.springboot.mybatis.service;
import com.springboot.mybatis.pojo.User;
public interface MyBatisUserService {
public User getUser(Long id);
}
package com.springboot.mybatis.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.mybatis.dao.MybatisUserDao;
import com.springboot.mybatis.pojo.User;
import com.springboot.mybatis.service.MyBatisUserService;
@Service
public class MyBatisUserServiceImpl implements MyBatisUserService {
@Autowired
private MybatisUserDao mybatisUserDao = null;
@Override
public User getUser(Long id) {
return mybatisUserDao.getUser(id);
}
}
⦁ 使用控制器测试Mybatis接口
package com.springboot.mybatis.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springboot.mybatis.pojo.User;
import com.springboot.mybatis.service.MyBatisUserService;
import com.springboot.mybatis.service.impl.MyBatisUserServiceImpl;
@Controller
@RequestMapping("/mybatis")
public class MyBatisController {
@Autowired
private MyBatisUserService myBatisUserService;
@RequestMapping("/getUser")
@ResponseBody
public User getUser(Long id) {
return myBatisUserService.getUser(id);
}
}
⦁ Spring Boot 的主类
package com.springboot.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Repository;
//定义Mybatis的扫描
@MapperScan(basePackages = "com.springboot.mybatis.dao", annotationClass = Repository.class)
//定义Spring Boot扫描包路径
@SpringBootApplication(scanBasePackages = { "com.springboot.mybatis" })
public class SpringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
}
⦁ 配置文件
#配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#指定数据库连接池类型
spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
#最大等待连接中的数量,设0为没有限制
spring.datasource.dbcp2.max-idle=10
#最大连接活动数
spring.datasource.dbcp2.max-total=50
#最大等待毫秒数,单位为ms,超过时间会出错信息
spring.datasource.dbcp2.max-wait-millis=10000
#数据库连接池初始化连接数
spring.datasource.dbcp2.initial-size=5
#MyBatis 映射文件通配
mybatis.mapper-locations=classpath:com/springboot/mybatis/mapper/*.xml
#MyBatis扫描别名包,和注解@Alias连用
mybatis.type-aliases-package=com.springboot.mybatis.pojo
#配置typeHandler的扫描
mybatis.type-handlers-package=com.springboot.mybatis.typehandler
#日志配置
logging.level.root=DEBUG
logging.level.org.springframework=DEBUG
logging.level.org.org.org.mybatis=DEBUG
⦁ 访问