博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件+SpringBoot整合Mybatis-plus...
阅读量:4657 次
发布时间:2019-06-09

本文共 10045 字,大约阅读时间需要 33 分钟。

package com.zsl.service.impl;import com.zsl.mapper.UserMapper;import com.zsl.pojo.User;import com.zsl.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServiceImpl implements UserService {    @Autowired    private UserMapper userMapper;//报错不影响    /*    * 增加/修改用户    * */    @Override    public String insertUser(User user) {        //增加用户        if(user.getUserId() == null){            Integer i = userMapper.insert(user);            if(i != 0 && i != null){                return "success";            }        }        return null;    }    @Override    public List
selectAllUser() { //PageHelper.startPage(1, 3); List
list = userMapper.selectAllUser(); return list; }}

 

我们这一一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件。然后再使用阿里巴巴提供的开源连接池druid,这个连接池的好处我就不说了,集合了所有连接池的好处,并且还提供了监控等功能,加大了可扩展性等等。

  1. 创建一个springboot项目:

2.可以看到的是我们除了引入web依赖之外还引入了三个依赖,分别是MySQL,JDBC,Mybatis,我们如下观看一下完整的依赖情况:

4.0.0
com.example
springboot-mybatis02
0.0.1-SNAPSHOT
jar
springboot-mybatis02
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
runtime
com.alibaba
druid-spring-boot-starter
1.1.1
      
    
    
org.springframework.boot
    
spring-boot-devtools
    
    
    
    
org.springframework.boot
    
spring-boot-starter-thymeleaf
    
com.github.pagehelper
pagehelper-spring-boot-starter
4.1.6
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true

3.依赖引入完成以后我们再对application.properties配置文件进行编辑:

#设置访问端口server.port=80#thymeleaf配置,这里是可以省略的,因为默认配置已经足够#关闭缓存,及时刷新页面,这一点很重要spring.thymeleaf.cache=false#注释的部分是Thymeleaf默认的配置,如有其它需求可以自行更改#spring.thymeleaf.prefix=classpath:/templates/#spring.thymeleaf.suffix=.html#spring.thymeleaf.mode=HTML5#spring.thymeleaf.encoding=UTF-8#spring.thymeleaf.servlet.content-type=text/html#设置热部署#开启热部署spring.devtools.restart.enabled=true#重启范围spring.devtools.restart.additional-paths=src/main/java#设置数据源#数据库连接用户名spring.datasource.username=root#数据库连接密码spring.datasource.password=123#驱动spring.datasource.driver-class-name= com.mysql.jdbc.Driver#数据库连接路径spring.datasource.url=jdbc:mysql://localhost:3306/bysj#连接池类型spring.datasource.type=com.alibaba.druid.pool.DruidDataSource#连接池配置,因为springboot默认是开启了连接池的,它有默认配置,这一段可以忽略# 初始化大小,最小,最大spring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20# 配置获取连接等待超时的时间spring.datasource.maxWait=60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒spring.datasource.timeBetweenEvictionRunsMillis=60000# 配置一个连接在池中最小生存的时间,单位是毫秒spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=false# 打开PSCache,并且指定每个连接上PSCache的大小spring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙spring.datasource.filters=stat,wall,log4j# 通过connectProperties属性来打开mergeSql功能;慢SQL记录spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500#配置mybatismybatis.mapper-location=classpath:mapping/*.xml#全局的映射,不用在xml文件写实体类的全路径mybatis.type-aliases-package=com.zsl.pojo#开启驼峰映射  mybatis.configuration.map-underscore-to-camel-case=true#配置分页插件#pagehelper分页插件  pagehelper.helper-dialect=mysql  pagehelper.reasonable=true  pagehelper.support-methods-arguments=true  pagehelper.params=count=countSql

4. 配置generator自动生成代码:文件放置的位置就在pom.xml文件里面一如插件的位置${basedir}/src/main/resources/generator/generatorConfig.xml

5.创建数据库以及表格

CREATE DATABASE bysj;CREATE TABLE user(  user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,  user_name VARCHAR(255) NOT NULL ,  password VARCHAR(255) NOT NULL ,  phone VARCHAR(255) NOT NULL) ENGINE=INNODB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8;

6. 操作IDEA生成代码:Run-->Edit Configurations

回到下图所示页面的时候点击三角即可:

7.查看项目情况以及生成代码:

7.2:生成的pojo实体类:

7.3生成的mapper的xml文件:在这里务必要注意namespace是否正确,请注意查看这一点

user_id, user_name, password, phone
   
   
delete from user where user_id = #{userId,jdbcType=INTEGER}
insert into user (user_id, user_name, password, phone) values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR})
insert into user
user_id,
user_name,
password,
phone,
#{userId,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
update user
user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
where user_id = #{userId,jdbcType=INTEGER}
update user set user_name = #{userName,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, phone = #{phone,jdbcType=VARCHAR} where user_id = #{userId,jdbcType=INTEGER}

7.4项目的总体结构:

7.5 自己用于测试分页,驼峰映射,实体类映射等配置的controller,service

package com.zsl.controller;import com.zsl.pojo.User;import com.zsl.service.impl.UserServiceImpl;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;import java.util.List;@Controllerpublic class Userontroller {    @Resource    private UserServiceImpl userService;    //增加用户    @ResponseBody    @RequestMapping("/insertUser")    public String insertUser(User user){        return userService.insertUser(user);    }    //查询所有的用户    @ResponseBody    @RequestMapping("/selectAllUser")    public String getAllUser(){        List
list = userService.selectAllUser(); System.out.println(list.size()); System.out.println(list); System.out.println(list.get(1).getUserId()); return null; }}

注意:在实验的时候务必要在启动类上加上MapperScna注解,指定扫描mapper文件,或者可以在每个mapper文件上加mapper注解,不过这样太麻烦了:

 加依赖

com.baomidou
mybatis-plus-boot-starter
3.0.1

mapper继承即可,相当于通用mapper不需要任何配置爽!

转载于:https://www.cnblogs.com/h-c-g/p/10846553.html

你可能感兴趣的文章
[HNOI2008]明明的烦恼
查看>>
Navicat不能连接linux数据库问题
查看>>
centos7关闭防火墙
查看>>
《C#高级编程》 读书笔记 -索引
查看>>
session cookie原理及应用
查看>>
ID3算法详解
查看>>
BZOJ1925: [Sdoi2010]地精部落
查看>>
学习进度条第十一周
查看>>
linux常用命令
查看>>
python 之Twsited
查看>>
设置SQL PLUS环境
查看>>
关于虚拟机VM
查看>>
eclipse、tomca和jvm的相关内存配置
查看>>
python的迭代器
查看>>
spy memcached spring demo
查看>>
Python基础语法
查看>>
4.1.1 硬币游戏
查看>>
获取服务器信息
查看>>
JavaScript_5_对象
查看>>
MyBatis总结五:#{}和${}的用法和区别
查看>>