今天抽点时间做了一下关于springboot和mybatis整合相关知识,阅读本文章默认有springboot和mybatis的基础。
首先先创建一个springboot工程,导入相应的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
创建实体类和数据库表
package com.example.demo.entity;
/**
* Created by linjiaming
*/
public class User {
public Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
分别创建Controller,Service和Dao
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by linjiaming
*/
@RestController
public class UserController {
@Resource
UserService userService;
@RequestMapping("user")
public User hello(@RequestParam(value = "id") Integer id){
User info = userService.getUserInfoById(id);
return info;
}
}
package com.example.demo.service.impl;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
/**
* Created by linjiaming
*/
@Service("UserService")
public class UserServiceImpl implements UserService {
@Resource
UserDao userDao;
@Override
public User getUserInfoById(Integer id) {
User info = userDao.getUserInfoById(id);
return info;
}
}
package com.example.demo.dao;
import com.example.demo.entity.User;
import org.springframework.stereotype.Repository;
/**
* Created by linjiaming
*/
@Repository
public interface UserDao {
User getUserInfoById(Integer id);
}
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">
<!--namespace 对应接口名
-->
<mapper namespace="com.example.demo.dao.UserDao" >
<!--数据库表列名和实体属性相绑定-->
<resultMap id="BaseResultMap" type="com.example.demo.entity.User" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, name, age
</sql>
<!--id 为接口对应方法名,parameterType 接口入参类型 resultMap 结果类型-->
<select id="getUserInfoById" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_user
where id = #{id,jdbcType=INTEGER}
</select>
</mapper>
配置文件属性
server.port=8080
#mysql6以上版本使用com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/fulan?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#mapper文件位置
mybatis.mapperLocations=classpath:mappers/*.xml
启动类上添加接口扫描注解
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.dao")//扫描dao接口
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}