引入pom包
<!--引用mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!--引用mybatis-->
配置yml
mybatis:
mapper-locations: classpath*:mapping/*Mapper.xml
type-aliases-package: com.big.fly.object
MyBatis-Spring-Boot-Application的配置参数也是保存在application.properties文件中,使用前缀 mybatis 。
Property | Description |
---|---|
config-location | MyBatis xml config file (optional) |
mapper-locations | Mapper xml config files (optional) |
type-aliases-package | Package to search for type aliases (optional) |
type-handlers-package | Package to search for type aliases (optional) |
executor-type | Executor type: SIMPLE, REUSE, BATCH (optional) |
configuration | A MyBatis Configuration bean. About available properties see the MyBatis reference page. NOTE This property cannot use at the same time with the config-location. |
(Starter)设置mapper有两种方法:
①使用config-location指定一个config xml,在里面设置 mapper 和 alias 。见例子1。
②使用type-aliases-package,需要配合自动扫描Mappers使用。
针对第二种,需要注意的是,如果想要自动扫描Mappers,需要在Mapper接口上标注@Mapper,否则失败。另外,还需要在application.properties文件中声明:mybatis.type-aliases-package 。
mapper-locations这个配置参数仅当mapper xml与mapper class不在同一个目录下时有效。所以一般可以忽略。
启动类添加MapperScan
package com.big.fly;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling//在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置
@MapperScan({"com.big.fly.mapper"})//将项目中对应的mapper类的路径加进来就可以了
public class FlyApplication {
public static void main(String[] args) {
SpringApplication.run(FlyApplication.class, args);
}
}
建立实例对象User
package com.big.fly.object;
/**
* Created by Administrator on 2018/4/15.
*/
public class User {
private Integer userId;
private String userName;
private String password;
private String phone;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
}
创建Mapper层接口
package com.big.fly.mapper;
/**
* Created by Administrator on 2018/4/15.
*/
import com.big.fly.object.User;
import java.util.List;
public interface UserMapper {
int deleteByPrimaryKey(Integer userId);
int insert(User record);
int insertSelective(User record);
int selectByPrimaryKey(Integer userId);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
//这个方式我自己加的
List<User> selectAllUser();
}
添加mapping:UserMapper.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.big.fly.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.big.fly.object.User" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
user_id, user_name, password, phone
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_user
where user_id = #{userId,jdbcType=INTEGER}
</select>
<!-- 这个方法是我自己加的 -->
<select id="selectAllUser" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_user
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_user
where user_id = #{userId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.big.fly.object.User" >
insert into t_user (user_id, user_name, password,
phone)
values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.big.fly.object.User" >
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userId != null" >
user_id,
</if>
<if test="userName != null" >
user_name,
</if>
<if test="password != null" >
password,
</if>
<if test="phone != null" >
phone,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userId != null" >
#{userId,jdbcType=INTEGER},
</if>
<if test="userName != null" >
#{userName,jdbcType=VARCHAR},
</if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
#{phone,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.big.fly.object.User" >
update t_user
<set >
<if test="userName != null" >
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
phone = #{phone,jdbcType=VARCHAR},
</if>
</set>
where user_id = #{userId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.big.fly.object.User" >
update t_user
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR}
where user_id = #{userId,jdbcType=INTEGER}
</update>
</mapper>
添加service
package com.big.fly.service;
/**
* Created by Administrator on 2018/4/15.
*/
import com.big.fly.object.User;
import java.util.List;
/**
* Created by Administrator on 2017/8/16.
*/
public interface UserService {
int addUser(User user);
List<User> findAllUser(int pageNum, int pageSize);
}
package com.big.fly.service.impl;
/**
* Created by Administrator on 2018/4/15.
*/
import com.big.fly.mapper.UserMapper;
import com.big.fly.object.User;
import com.big.fly.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/*import com.github.pagehelper.PageHelper;*/
/**
* Created by Administrator on 2017/8/16.
*/
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;//这里会报错,但是并不会影响Settings - Editor - Inspections - Spring - Spring Core - Code - Autowiring for Bean Class - disable(idea2017好像是把√去掉)
@Override
public int addUser(User user) {
return userMapper.insertSelective(user);
}
/*
* 这个方法中用到了我们开头配置依赖的分页插件pagehelper
* 很简单,只需要在service层传入参数,然后将参数传递给一个插件的一个静态方法即可;
* pageNum 开始页数
* pageSize 每页显示的数据条数
* */
@Override
public List<User> findAllUser(int pageNum, int pageSize) {
//将参数传给这个方法就可以实现物理分页了,非常简单。
/* PageHelper.startPage(pageNum, pageSize);*/
return userMapper.selectAllUser();
}
}
添加userController调用接口
package com.big.fly.controller;
import com.big.fly.object.User;
import com.big.fly.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2018/5/1.
*/
@RestController
public class userController {
@Autowired
private UserService userService;
/**
* mybatis的测试
*
*/
@ResponseBody
@RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
public int addUser(User user){
return userService.addUser(user);
}
@ResponseBody
@RequestMapping(value = "/all", produces = {"application/json;charset=UTF-8"})
public Object findAllUser(){
return userService.findAllUser(1,1);
}
}
代码层次结构如下
测试结果