spingBoot整合mybatis+generator+pageHelper
环境/版本一览:
- 开发工具:Intellij IDEA 2018.1.4
- springboot: 2.0.4.RELEASE
- jdk:1.8.0_40
- maven:3.3.9
- alibaba Druid 数据库连接池:1.1.9
额外功能:
- PageHelper 分页插件
- mybatis generator 自动生成代码插件
开始搭建:
创建项目:
看一下文件结构:
查看一下pom.xml:
补齐依赖:
项目不使用application.properties文件 而使用更加简洁的application.yml文件:
将原有的resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件,
文件的内容如下:
注意application.yml 要放在resource的一级目录下,否则会找不到,报错。
application.yml
使用mybatis generator 自动生成代码:
- 配置pom.xml中generator 插件所对应的配置文件 ${basedir}/src/main/resources/generator/generatorConfig.xml
generatorConfig.xml :
- 点击run-Edit Configurations
运行 :
注意!!!同一张表一定不要运行多次,因为mapper的映射文件中会生成多次的代码,导致报错,切记
最后生成的文件及结构:
UserMapper:
User :
1 package com.denwgei.springbootmybatis.model; 2 3 import java.util.Date; 4 5 public class User { 6 private Integer id; 7 8 private Integer userId; 9 10 private String userName; 11 12 private String passWord; 13 14 private String state; 15 16 private String type; 17 18 private Date updateTime; 19 20 private Date createTime; 21 22 public Integer getId() { 23 return id; 24 } 25 26 public void setId(Integer id) { 27 this.id = id; 28 } 29 30 public Integer getUserId() { 31 return userId; 32 } 33 34 public void setUserId(Integer userId) { 35 this.userId = userId; 36 } 37 38 public String getUserName() { 39 return userName; 40 } 41 42 public void setUserName(String userName) { 43 this.userName = userName == null ? null : userName.trim(); 44 } 45 46 public String getPassWord() { 47 return passWord; 48 } 49 50 public void setPassWord(String passWord) { 51 this.passWord = passWord == null ? null : passWord.trim(); 52 } 53 54 public String getState() { 55 return state; 56 } 57 58 public void setState(String state) { 59 this.state = state == null ? null : state.trim(); 60 } 61 62 public String getType() { 63 return type; 64 } 65 66 public void setType(String type) { 67 this.type = type == null ? null : type.trim(); 68 } 69 70 public Date getUpdateTime() { 71 return updateTime; 72 } 73 74 public void setUpdateTime(Date updateTime) { 75 this.updateTime = updateTime; 76 } 77 78 public Date getCreateTime() { 79 return createTime; 80 } 81 82 public void setCreateTime(Date createTime) { 83 this.createTime = createTime; 84 } 85 }
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.dengwei.day01springboot.dao.UserMapper" > <resultMap id="BaseResultMap" type="com.dengwei.day01springboot.model.User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="user_id" property="userId" jdbcType="INTEGER" /> <result column="user_name" property="userName" jdbcType="VARCHAR" /> <result column="pass_word" property="passWord" jdbcType="VARCHAR" /> <result column="state" property="state" jdbcType="VARCHAR" /> <result column="type" property="type" jdbcType="VARCHAR" /> <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" /> <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> </resultMap> <sql id="Base_Column_List" > id, user_id, user_name, pass_word, state, type, update_time, create_time </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from user where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from user where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.dengwei.day01springboot.model.User" > insert into user (id, user_id, user_name, pass_word, state, type, update_time, create_time) values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{passWord,jdbcType=VARCHAR}, #{state,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}, #{createTime,jdbcType=TIMESTAMP}) </insert> <insert id="insertSelective" parameterType="com.dengwei.day01springboot.model.User" > insert into user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="userId != null" > user_id, </if> <if test="userName != null" > user_name, </if> <if test="passWord != null" > pass_word, </if> <if test="state != null" > state, </if> <if test="type != null" > type, </if> <if test="updateTime != null" > update_time, </if> <if test="createTime != null" > create_time, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <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="state != null" > #{state,jdbcType=VARCHAR}, </if> <if test="type != null" > #{type,jdbcType=VARCHAR}, </if> <if test="updateTime != null" > #{updateTime,jdbcType=TIMESTAMP}, </if> <if test="createTime != null" > #{createTime,jdbcType=TIMESTAMP}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.dengwei.day01springboot.model.User" > update user <set > <if test="userId != null" > user_id = #{userId,jdbcType=INTEGER}, </if> <if test="userName != null" > user_name = #{userName,jdbcType=VARCHAR}, </if> <if test="passWord != null" > pass_word = #{passWord,jdbcType=VARCHAR}, </if> <if test="state != null" > state = #{state,jdbcType=VARCHAR}, </if> <if test="type != null" > type = #{type,jdbcType=VARCHAR}, </if> <if test="updateTime != null" > update_time = #{updateTime,jdbcType=TIMESTAMP}, </if> <if test="createTime != null" > create_time = #{createTime,jdbcType=TIMESTAMP}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.dengwei.day01springboot.model.User" > update user set user_id = #{userId,jdbcType=INTEGER}, user_name = #{userName,jdbcType=VARCHAR}, pass_word = #{passWord,jdbcType=VARCHAR}, state = #{state,jdbcType=VARCHAR}, type = #{type,jdbcType=VARCHAR}, update_time = #{updateTime,jdbcType=TIMESTAMP}, create_time = #{createTime,jdbcType=TIMESTAMP} where id = #{id,jdbcType=INTEGER} </update> </mapper>
项目启动类:
package com.denwgei.springbootmybatis; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.denwgei.springbootmybatis.Dao") public class SpringbootmybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringbootmybatisApplication.class, args); } }
注意:@MapperScan("com.winter.mapper")
这个注解非常的关键,这个对应了项目中mapper(dao)所对应的包路径,很多同学就是这里忘了加导致异常的
新建service层 :
IUserService :
1 package com.denwgei.springbootmybatis.service; 2 3 import com.denwgei.springbootmybatis.model.User; 4 import com.github.pagehelper.PageInfo; 5 6 public interface IUserService { 7 public PageInfo<User> queryAll(int pageNum, int pageSize); 8 }
impl :UserService :
userController :
启动springBoot的启动程序:
启动成功:
好的,试试吧!!!!
有个错误说下
mybatis Field xxxMapper in xxxx required a bean of type 'XXXMapper' that could not be found.
原文: https://blog.csdn.net/j754379117/article/details/71639043/
方法1:application类加注释:@MapperScan(basePackages = { "xxx.xxx.mapper" }, sqlSessionFactoryRef = "sqlSessionFactory"),表示扫描xx.xx.mapper包下的所有mapper。 方法2:直接在你生成出来的xxxMapper.java类上加@Mapper标签。
js 多个数组取交集
macOS APP 窗口焦点监听
proxifier注册码
天才算法之睡眠排序(C#实现)
Tomcat 7使用AJP协议设置问题
nginx启动报错(1113: No mapping for the Unicode character exists in the target multi-byte code page)
八皇后的n种放置方法
insufficient permission for adding an object to repository database .git/objects
centos下搭建php开发环境(lamp)