SpringBoot
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.lanougroupId>
<artifactId>sshartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>sshname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.2.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.9version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.47version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.41version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.2version>
<scope>compilescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>1.2.4version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.2version>
<configuration>
<verbose>trueverbose>
<overwrite>trueoverwrite>
configuration>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.29version>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapperartifactId>
<version>4.0.0version>
dependency>
dependencies>
plugin>
plugins>
build>
project>
generatorConfig.xml
<generatorConfiguration>
<context id="default" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.lanou.util.MyMapper"/>
plugin>
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
commentGenerator>
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/MVCDB?characterEncoding=utf-8"
userId="root"
password="123456">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
javaTypeResolver>
<javaModelGenerator targetPackage="com.lanou.model" targetProject="./src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="constructorBased" value="true"/>
<property name="trimStrings" value="true"/>
<property name="immutable" value="false"/>
javaModelGenerator>
<sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
<property name="enableSubPackages" value="false"/>
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.lanou.dao" targetProject="./src/main/java">
<property name="enableSubPackages" value="false" />
javaClientGenerator>
<table tableName="User" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
context>
generatorConfiguration>
反向生成
双击击mybatis-generator下的mybatis-generator:generate
会自动生成mappers包与此包下的UserMapper.xml映射文件
还会自动生成dao包下的UserMapper接口,此接口与UserMapper.xml相对应
还好根据数据库中的表自动生成model包下的实体类
自动生成的mappers下的UserMapper.xml映射文件
<mapper namespace="com.lanou.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.lanou.model.User" >
<constructor >
<idArg column="u_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="u_name" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="u_password" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="u_gender" jdbcType="VARCHAR" javaType="java.lang.String" />
constructor>
resultMap>
mapper>
自动生成的User实体类
package com.lanou.model;
import javax.persistence.*;
public class User {
@Id
@Column(name = "u_id")
private Integer uId;
@Column(name = "u_name")
private String uName;
@Column(name = "u_password")
private String uPassword;
@Column(name = "u_gender")
private String uGender;
public User(Integer uId, String uName, String uPassword, String uGender) {
this.uId = uId;
this.uName = uName;
this.uPassword = uPassword;
this.uGender = uGender;
}
public User() {
super();
}
/**
* @return u_id
*/
public Integer getuId() {
return uId;
}
/**
* @param uId
*/
public void setuId(Integer uId) {
this.uId = uId;
}
/**
* @return u_name
*/
public String getuName() {
return uName;
}
/**
* @param uName
*/
public void setuName(String uName) {
this.uName = uName == null ? null : uName.trim();
}
/**
* @return u_password
*/
public String getuPassword() {
return uPassword;
}
/**
* @param uPassword
*/
public void setuPassword(String uPassword) {
this.uPassword = uPassword == null ? null : uPassword.trim();
}
/**
* @return u_gender
*/
public String getuGender() {
return uGender;
}
/**
* @param uGender
*/
public void setuGender(String uGender) {
this.uGender = uGender == null ? null : uGender.trim();
}
}
自动生成的dao层中的UserMapper
package com.lanou.dao;
import com.lanou.model.User;
import com.lanou.util.MyMapper;
public interface UserMapper extends MyMapper<User> {
}
继承是生成时自带的 不过需要在事先配置的文件中进行配置说明
MyMapper这个接口需要自己事先创建,MyMapper继承了两个接口
为了合并这两个接口中的方法
自己创建的util包下的MyMapper接口
package com.lanou.util;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper<T> extends MySqlMapper<T>,Mapper<T> {
}
自己创建的application.yml
server:
port: 8090
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/MVCDB?characterEncoding=utf-8
username: root
password: 123456
mybatis:
type-aliases-package: com.lanou.model
mapper-locations: classpath:mappers/*.xml
mapper:
mappers: com.lanou.util.MyMapper
not-empty: false
identity: MYSQL
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
UserControllerl类
package com.lanou.controller
import com.github.pagehelper.PageHelper
import com.lanou.dao.UserMapper
import com.lanou.model.ConfigBean
import com.lanou.model.User
import com.mysql.jdbc.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import tk.mybatis.mapper.entity.Example
import java.util.List
@Controller
public class UserController {
@Autowired
private UserMapper userMapper
@Autowired
private ConfigBean configBean
@Value(value = "${page}")
private Integer size
//@GetMapping(value = "/pages")
//@PostMapping
@ResponseBody
@RequestMapping("/pages")
public List selectPage(Integer page,User user){
System.out.println("configBean"+configBean)
PageHelper.startPage(page,size)
//告诉是用哪个类来封装
Example example = new Example(User.class)
//获的criteria里的查询方法
Example.Criteria criteria = example.createCriteria()
if (!StringUtils.isEmptyOrWhitespaceOnly(user.getName())){
criteria.andLike("name","%"+user.getName()+"%")
}
example.orderBy("age").desc()
return userMapper.selectByExample(example)
}
// @ResponseBody
// @RequestMapping("/select")
// public List select(User user){
//
// System.out.println("la la la la la ...")
// System.out.println("user:"+user)
//
//
// return userMapper.selectB()
//
// }
@ResponseBody
@RequestMapping("/jiadelete")
public int jiadelete(User user){
System.out.println("la la la la la ...")
System.out.println("user:"+user)
user.setUserDeleteld(1)
return userMapper.updateByPrimaryKeySelective(user)
}
@ResponseBody
@RequestMapping("/delete")
public int delete(User user){
System.out.println("la la la la la ...")
System.out.println("user:"+user)
return userMapper.deleteByPrimaryKey(user.getId())
}
@ResponseBody
@RequestMapping("/update")
public int update(User user){
System.out.println("la la la la la ...")
System.out.println("user:"+user)
return userMapper.updateByPrimaryKeySelective(user)
}
@ResponseBody
@RequestMapping("/insert")
public int insert(User user){
System.out.println("la la la la la ...")
System.out.println("user:"+user)
return userMapper.insert(user)
}
@ResponseBody
@RequestMapping("/findUser")
public List hellow(){
System.out.println("la la la la la ...")
return userMapper.selectAll()
}
@ResponseBody
@RequestMapping("/lala")
public String hello(){
System.out.println("la la la la la ...")
return "name"
}
}
定义userMapper需要注解注入 这时UserMapper并没有被引入Spring中 因此需要加上注解
让Spring管理UserMapper
SshApplication
package com.lanou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.lanou.dao")
public class SshApplication {
public static void main(String[] args) {
SpringApplication.run(SshApplication.class, args);
}
}