博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot2 整合(一)Mybatis (特别完整!)
阅读量:6715 次
发布时间:2019-06-25

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

 

大概介绍下流程:

  1. 借助idea实现mybatis逆向工程
  2. 用xml配置实现整合
  3. 用cmd命令行实现mybatis逆向工程
  4. 用mapping.xml配置实现数据交互
  5. 用注解的方式实现数据交互

首先我的开发环境:

jdk1.8+maven3+IDEA

1. mybatis逆向攻城

逆向工程方式很多,我目前接触到的就两种,一种是借助于ide开发工具,一种是在cmd中执行命令。(其实二者原理都一样,都是执行maven的generator命令,具体请看下文)。

1. 完善pom文件
4.0.0
springboot-mybatis
springboot-mybatis
0.0.1-SNAPSHOT
jar
springboot-mybatis
Demo project for Spring Boot
springboot-integration
springboot-integration
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
mysql
mysql-connector-java
5.1.35
com.alibaba
druid
1.0.11
com.alibaba
druid-spring-boot-starter
1.1.0
junit
junit
test
com.github.pagehelper
pagehelper
5.0.4
springboot-mybatis
org.apache.maven.plugins
maven-surefire-plugin
true
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
true
true
pom.xml
2. 逆向所需配置文件generatorConfig.xml
generatorConfig.xml
3. 利用IDE创建逆向工程启动类

4. add一个Maven configuration

5. 我的数据库和表结构

6. application配置文件
server:port: 8080 spring:  datasource:  name: test   url: jdbc:mysql://127.0.0.1:3306/user   username: root  password: root   # druid 连接池    type: com.alibaba.druid.pool.DruidDataSource   driver-class-name: com.mysql.jdbc.Driver   filters: stat   maxActive: 20   initialSize: 1   maxWait: 60000   minIdle: 1   timeBetweenEvictionRunsMillis: 60000   minEvictableIdleTimeMillis: 300000   validationQuery: select 'x'   testWhileIdle: true   testOnBorrow: false   testOnReturn: false   poolPreparedStatements: true   maxOpenPreparedStatements: 20 mybatis:  mapper-locations: classpath:mapping/*.xml   type-aliases-package: com.fant.model   #pagehelper分页插件 pagehelper:   helperDialect: mysql   reasonable: true   supportMethodsArguments: true   params: count=countSql
application.yml

运行generator工程 自动生成代码

 

 

生成的文件

User.java
package com.fantj.model;import java.util.Date;public class User {    private Integer id;    private String username;    private Date birthday;    private String sex;    private String address;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username == null ? null : username.trim();    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex == null ? null : sex.trim();    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address == null ? null : address.trim();    }}
User.java
UserMapper .java
package com.fantj.mapper;import com.fantj.model.User;import java.util.List;public interface UserMapper {    int deleteByPrimaryKey(Integer id);    int insert(User record);    int insertSelective(User record);    User selectByPrimaryKey(Integer id);    int updateByPrimaryKeySelective(User record);    int updateByPrimaryKey(User record);    List
selectAll();}
userMapper.java
UserMapper.xml
id, username, birthday, sex, address
delete from user where id = #{id,jdbcType=INTEGER}
insert into user (id, username, birthday, sex, address) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, #{sex,jdbcType=CHAR}, #{address,jdbcType=VARCHAR})
insert into user
id,
username,
birthday,
sex,
address,
#{id,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{birthday,jdbcType=DATE},
#{sex,jdbcType=CHAR},
#{address,jdbcType=VARCHAR},
update user
username = #{username,jdbcType=VARCHAR},
birthday = #{birthday,jdbcType=DATE},
sex = #{sex,jdbcType=CHAR},
address = #{address,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update user set username = #{username,jdbcType=VARCHAR}, birthday = #{birthday,jdbcType=DATE}, sex = #{sex,jdbcType=CHAR}, address = #{address,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER}
userMapper.xml

修改启动类

逆向生成代码后,我们还需要在启动类上添加一个@MapperScan("com.fantj.mapper")注解,告诉我们的Mapper需要扫描的包,这样就不用每个Mapper上都添加@Mapper注解了

package com.fantj;import com.fantj.util.Params;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Configuration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@SpringBootApplication@MapperScan("com.fantj.mapper")public class MybatisApplication {    public static void main(String[] args) {        SpringApplication.run(MybatisApplication.class, args);    }}
App.java

完善controller和service

UserController.java
package com.fantj.controller;import com.fantj.model.User;import com.fantj.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import javax.jws.soap.SOAPBinding;import java.util.Date;import java.util.List;@RestController@RequestMapping("/user")public class UserController {    @Autowired    private UserService userService;    @RequestMapping(method = RequestMethod.GET, value = "/delete/{id}")    public void delete(@PathVariable("id") int id) {        userService.delete(id);    }    @RequestMapping(method = RequestMethod.POST, value = "/insert")    public void insert(User user) {        userService.insert(user);    }    @RequestMapping(method = RequestMethod.POST, value = "/update/{id}")    public void update(@RequestParam User user) {        userService.update(user);    }    @RequestMapping(method = RequestMethod.GET, value = "/{id}/select")    public User select(@PathVariable("id") int id) {        return userService.selectById(id);    }    @RequestMapping(method = RequestMethod.GET, value = "/selectAll/{pageNum}/{pageSize}")    public List
selectAll(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) { return userService.selectAll(pageNum, pageSize); }}
UserController.java
UserService.java
package com.fantj.service;import com.fantj.model.User;import java.util.List;public interface UserService {    /**     * 删除     */    public void delete(int id);    /**     * 增加     */    public void insert(User user);    /**     * 更新     */    public int update(User user);    /**     * 查询单个     */    public User selectById(int id);    /**     * 查询全部列表     */    public List
selectAll(int pageNum, int pageSize);}
UserService.java
UserServiceImpl .java
package com.fantj.service.impl;import com.fantj.mapper.UserMapper;import com.fantj.model.User;import com.fantj.service.UserService;import com.github.pagehelper.PageHelper;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;    /**     * 删除 * * @param id     */    @Override    public void delete(int id) {        userMapper.deleteByPrimaryKey(id);    }    /**     * 增加 * * @param user     */    @Override    public void insert(User user) {        userMapper.insert(user);    }    /**     * 更新 * * @param user     */    @Override    public int update(User user) {        return userMapper.updateByPrimaryKey(user);    }    /**     * 查询单个 * * @param id     */    @Override    public User selectById(int id) {        return userMapper.selectByPrimaryKey(id);    }    /**     * 查询全部列表,并做分页 * * @param pageNum 开始页数 * @param pageSize 每页显示的数据条数     */    @Override    public List
selectAll(int pageNum, int pageSize) { //将参数传给这个方法就可以实现物理分页了,非常简单。 PageHelper.startPage(pageNum,pageSize); return userMapper.selectAll(); } }
UserServiceImpl.java

 

 

转载地址:http://ieelo.baihongyu.com/

你可能感兴趣的文章
数据恢复软件哪个好
查看>>
『火车进出栈问题 卡特兰数』
查看>>
第四天:HTTP&Tomcat
查看>>
python 文件和路径操作函数小结
查看>>
条件+努力=?
查看>>
HBase分布式安装
查看>>
随笔-文件的读写
查看>>
tcp 状态以及三次握手
查看>>
我的友情链接
查看>>
WAITED TOO LONG FOR A ROW CACHE ENQUEUE LOCK!的分析
查看>>
nginx禁止ip直接访问
查看>>
hadoop常用服务管理命令
查看>>
10.28 rsync工具10.29-10.30 rsync选项10.31 rsync通过ssh同步
查看>>
Fault,Error and Failure
查看>>
Go语言的通道(1)-无缓冲通道
查看>>
spring oauth从请求中获取token
查看>>
6.18docker(一)Compose 模板文件
查看>>
每天学点GDB 9
查看>>
为什么要用 /dev/null 2>&1 这样的写法
查看>>
AngularJs创建省,市,区的3级列表
查看>>