本文共 12603 字,大约阅读时间需要 42 分钟。
大概介绍下流程:
首先我的开发环境:
jdk1.8+maven3+IDEA逆向工程方式很多,我目前接触到的就两种,一种是借助于ide开发工具,一种是在cmd中执行命令。(其实二者原理都一样,都是执行maven的generator命令,具体请看下文)。
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
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
运行generator工程 自动生成代码
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(); }}
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); ListselectAll();}
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 where id = #{id,jdbcType=INTEGER} username = #{username,jdbcType=VARCHAR}, birthday = #{birthday,jdbcType=DATE}, sex = #{sex,jdbcType=CHAR}, address = #{address,jdbcType=VARCHAR}, update user set username = #{username,jdbcType=VARCHAR}, birthday = #{birthday,jdbcType=DATE}, sex = #{sex,jdbcType=CHAR}, address = #{address,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER}
逆向生成代码后,我们还需要在启动类上添加一个@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); }}
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 ListselectAll(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) { return userService.selectAll(pageNum, pageSize); }}
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 ListselectAll(int pageNum, int pageSize);}
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 ListselectAll(int pageNum, int pageSize) { //将参数传给这个方法就可以实现物理分页了,非常简单。 PageHelper.startPage(pageNum,pageSize); return userMapper.selectAll(); } }
转载地址:http://ieelo.baihongyu.com/