spingBoot整合mybatis+generator+pageHelper

 2023-09-09 阅读 27 评论 0

摘要:spingBoot整合mybatis+generator+pageHelper 环境/版本一览: 开发工具:Intellij IDEA 2018.1.4springboot:2.0.4.RELEASEjdk:1.8.0_40maven:3.3.9alibaba Druid 数据库连接池:1.1.9额外功能: PageHelper 分页插件mybatis generato

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 自动生成代码插件

 

开始搭建:

创建项目:

 

 

 

maven java。 

 

 

 

看一下文件结构:

Mybatis plus。 

 查看一下pom.xml:

补齐依赖:

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

 

 项目不使用application.properties文件 而使用更加简洁的application.yml文件: 
将原有的resource文件夹下的application.properties文件删除,创建一个新的application.yml配置文件, 
文件的内容如下:

  注意application.yml  要放在resource的一级目录下,否则会找不到,报错。

mybatis怎么用。 

 application.yml

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

 

 

使用mybatis generator 自动生成代码:

  • 配置pom.xml中generator 插件所对应的配置文件 ${basedir}/src/main/resources/generator/generatorConfig.xml

 

MyBatis。 

 generatorConfig.xml  :

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

 

 

  • 点击run-Edit Configurations

 

 

spring mybatis原理? 

 

 

     运行 :
注意!!!同一张表一定不要运行多次,因为mapper的映射文件中会生成多次的代码,导致报错,切记 

 

 

spring jdbc, 最后生成的文件及结构:

 

 UserMapper:

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

 

 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 }
复制代码

spring管理mybatis。 

 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 userwhere id = #{id,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from userwhere 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 userset 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>
复制代码

 

 

 

Spring boot、 

项目启动类:

 

复制代码
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 }
复制代码

为什么不用tkmybatis? 

impl :UserService :

 

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

 

userController  :

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

 

Springboot框架、 启动springBoot的启动程序:

 

 

 启动成功:

javacomparator。 

 好的,试试吧!!!!

有个错误说下 

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标签。

 

mybatis plus该不该用, 

分类: springBoot
标签: springBoot+mybatis+generator+helper

转载于:https://www.cnblogs.com/liyiren/p/10594028.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/31863.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息