增加mybatis样例
This commit is contained in:
parent
cc974943f2
commit
25ccae307c
71
mybatis-test/pom.xml
Normal file
71
mybatis-test/pom.xml
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.example</groupId>
|
||||||
|
<artifactId>mybatis-test</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>mybatis-test</name>
|
||||||
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.0.3.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>1.3.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!--ali连接池依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-starter</artifactId>
|
||||||
|
<version>1.1.9</version>
|
||||||
|
</dependency>
|
||||||
|
<!--分页依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.pagehelper</groupId>
|
||||||
|
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||||
|
<version>1.2.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.example.mybatistest;
|
||||||
|
|
||||||
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@MapperScan("com.example.mybatistest.dao")
|
||||||
|
public class MybatisTestApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(MybatisTestApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.example.mybatistest.controller;
|
||||||
|
|
||||||
|
import com.example.mybatistest.entity.User;
|
||||||
|
import com.example.mybatistest.service.UserService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
* Description:
|
||||||
|
* User: ${fxb}
|
||||||
|
* Email: fanxb.tl@gmail.com
|
||||||
|
* Date: 2018-07-30
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@GetMapping("/user/{userId}")
|
||||||
|
public User getUser(@PathVariable String userId){
|
||||||
|
return userService.getByUserId(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/user")
|
||||||
|
public List<User> getAll(){
|
||||||
|
return userService.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/user/page/{pageNum}")
|
||||||
|
public Object getPage(@PathVariable int pageNum,
|
||||||
|
@RequestParam(name = "pageSize",required = false,defaultValue = "10") int pageSize){
|
||||||
|
return userService.getAll(pageNum,pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/user")
|
||||||
|
public Object addOne(User user){
|
||||||
|
userService.insert(user);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.example.mybatistest.dao;
|
||||||
|
|
||||||
|
import com.example.mybatistest.entity.User;
|
||||||
|
import org.apache.ibatis.annotations.Insert;
|
||||||
|
import org.apache.ibatis.annotations.Options;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
* Description:
|
||||||
|
* User: ${fxb}
|
||||||
|
* Email: fanxb.tl@gmail.com
|
||||||
|
* Date: 2018-07-30
|
||||||
|
*/
|
||||||
|
public interface UserDao {
|
||||||
|
//插入用户
|
||||||
|
@Insert("insert into user(name,age,password) value(#{name},#{age},#{password})")
|
||||||
|
@Options(useGeneratedKeys=true,keyColumn="id",keyProperty="id")
|
||||||
|
int insert(User user);
|
||||||
|
//根据id查询
|
||||||
|
@Select("select * from user where id=#{id}")
|
||||||
|
User selectById(String id);
|
||||||
|
//查询所有
|
||||||
|
@Select("select * from user")
|
||||||
|
List<User> selectAll();
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.example.mybatistest.entity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
* Description:
|
||||||
|
* User: ${fxb}
|
||||||
|
* Email: fanxb.tl@gmail.com
|
||||||
|
* Date: 2018-07-30
|
||||||
|
*/
|
||||||
|
public class User {
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public User(int id, String name, int age, String password) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
public User(){}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.example.mybatistest.service;
|
||||||
|
|
||||||
|
import com.example.mybatistest.dao.UserDao;
|
||||||
|
import com.example.mybatistest.entity.User;
|
||||||
|
import com.github.pagehelper.PageHelper;
|
||||||
|
import com.github.pagehelper.PageInfo;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
* Description:
|
||||||
|
* User: ${fxb}
|
||||||
|
* Email: fanxb.tl@gmail.com
|
||||||
|
* Date: 2018-07-30
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class UserService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserDao userDao;
|
||||||
|
|
||||||
|
public User getByUserId(String id){
|
||||||
|
return userDao.selectById(id);
|
||||||
|
}
|
||||||
|
//获取全部用户
|
||||||
|
public List<User> getAll(){
|
||||||
|
return userDao.selectAll();
|
||||||
|
}
|
||||||
|
//测试分页
|
||||||
|
public PageInfo<User> getAll(int pageNum,int pageSize){
|
||||||
|
PageHelper.startPage(pageNum,pageSize);
|
||||||
|
List<User> users = userDao.selectAll();
|
||||||
|
System.out.println(users.size());
|
||||||
|
PageInfo<User> result = new PageInfo<>(users);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int insert(User user){
|
||||||
|
return userDao.insert(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
46
mybatis-test/src/main/resources/application.yml
Normal file
46
mybatis-test/src/main/resources/application.yml
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
mybatis:
|
||||||
|
#对应实体类路径
|
||||||
|
type-aliases-package: com.example.mybatistest.entity
|
||||||
|
#对应mapper映射文件路径
|
||||||
|
# mapper-locations: classpath:mapper/*.xml
|
||||||
|
|
||||||
|
#pagehelper配置
|
||||||
|
pagehelper:
|
||||||
|
helper-dialect: mysql
|
||||||
|
reasonable: true
|
||||||
|
support-methods-arguments: true
|
||||||
|
params: count=countSql
|
||||||
|
returnPageInfo: check
|
||||||
|
|
||||||
|
server:
|
||||||
|
port: 8081
|
||||||
|
|
||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
name: mysqlTest
|
||||||
|
type: com.alibaba.druid.pool.DruidDataSource
|
||||||
|
#druid相关配置
|
||||||
|
druid:
|
||||||
|
#监控拦截统计的filters
|
||||||
|
filters: stat
|
||||||
|
driver-class-name: com.mysql.jdbc.Driver
|
||||||
|
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true
|
||||||
|
username: root
|
||||||
|
password: 123456
|
||||||
|
#配置初始化大小,最小,最大
|
||||||
|
initial-size: 1
|
||||||
|
min-idle: 1
|
||||||
|
max-active: 20
|
||||||
|
#获取连接等待超时时间
|
||||||
|
max-wait: 6000
|
||||||
|
#间隔多久检测一次需要关闭的空闲连接
|
||||||
|
time-between-eviction-runs-millis: 60000
|
||||||
|
#一个连接在池中的最小生存时间
|
||||||
|
min-evictable-idle-time-millis: 300000
|
||||||
|
#打开PSCache,并指定每个连接上PSCache的大小。oracle设置为true,mysql设置为false。分库分表设置较多推荐设置
|
||||||
|
pool-prepared-statements: false
|
||||||
|
max-pool-prepared-statement-per-connection-size: 20
|
||||||
|
http:
|
||||||
|
encoding:
|
||||||
|
charset: utf-8
|
||||||
|
enabled: true
|
41
mybatis-test/src/main/resources/mapper/UserMapper.xml
Normal file
41
mybatis-test/src/main/resources/mapper/UserMapper.xml
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?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.example.mybatistest.dao.UserDao">
|
||||||
|
<sql id="BASE_TABLE">
|
||||||
|
user
|
||||||
|
</sql>
|
||||||
|
<sql id="BASE_COLUMN">
|
||||||
|
id,name,age,password
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<insert id="insert" parameterType="com.example.mybatistest.entity.User" useGeneratedKeys="true" keyProperty="id">
|
||||||
|
INSERT INTO <include refid="BASE_TABLE"/>
|
||||||
|
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||||
|
name,password,
|
||||||
|
<if test="age!=null">
|
||||||
|
age
|
||||||
|
</if>
|
||||||
|
</trim>
|
||||||
|
<trim prefix=" VALUE(" suffix=")" suffixOverrides=",">
|
||||||
|
#{name,jdbcType=VARCHAR},#{password},
|
||||||
|
<if test="age!=null">
|
||||||
|
#{age}
|
||||||
|
</if>
|
||||||
|
</trim>
|
||||||
|
</insert>
|
||||||
|
|
||||||
|
<select id="selectById" resultType="com.example.mybatistest.entity.User">
|
||||||
|
select
|
||||||
|
<include refid="BASE_COLUMN"/>
|
||||||
|
from
|
||||||
|
<include refid="BASE_TABLE"/>
|
||||||
|
where id=#{id}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="selectAll" resultType="com.example.mybatistest.entity.User">
|
||||||
|
select
|
||||||
|
<include refid="BASE_COLUMN"/>
|
||||||
|
from
|
||||||
|
<include refid="BASE_TABLE"/>
|
||||||
|
</select>
|
||||||
|
</mapper>
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.example.mybatistest;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class MybatisTestApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user