Skip to content

Commit 4daf8cf

Browse files
committed
thymeleaf 模块,redis 模块,mybatis 模块
0 parents  commit 4daf8cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2534
-0
lines changed

springboot-mybatis/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

springboot-mybatis/pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.tosea</groupId>
12+
<artifactId>springboot-mybatis</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-mybatis</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.mybatis.spring.boot</groupId>
28+
<artifactId>mybatis-spring-boot-starter</artifactId>
29+
<version>2.0.0</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>mysql</groupId>
34+
<artifactId>mysql-connector-java</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<optional>true</optional>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
<!-- mybatis启动器 -->
49+
<dependency>
50+
<groupId>org.mybatis.spring.boot</groupId>
51+
<artifactId>mybatis-spring-boot-starter</artifactId>
52+
<version>1.3.2</version>
53+
</dependency>
54+
<!-- 通用Mapper启动器 -->
55+
<dependency>
56+
<groupId>tk.mybatis</groupId>
57+
<artifactId>mapper-spring-boot-starter</artifactId>
58+
<version>2.1.0</version>
59+
</dependency>
60+
<!-- 分页助手启动器 -->
61+
<dependency>
62+
<groupId>com.github.pagehelper</groupId>
63+
<artifactId>pagehelper-spring-boot-starter</artifactId>
64+
<version>1.2.5</version>
65+
</dependency>
66+
</dependencies>
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.springframework.boot</groupId>
72+
<artifactId>spring-boot-maven-plugin</artifactId>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
77+
</project>

springboot-mybatis/sql/tb_user.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Navicat MySQL Data Transfer
3+
4+
Source Server : localhost
5+
Source Server Version : 50713
6+
Source Host : localhost:3306
7+
Source Database : springboot-learn
8+
9+
Target Server Type : MYSQL
10+
Target Server Version : 50713
11+
File Encoding : 65001
12+
13+
Date: 2019-04-02 13:35:56
14+
*/
15+
16+
SET FOREIGN_KEY_CHECKS=0;
17+
18+
-- ----------------------------
19+
-- Table structure for tb_user
20+
-- ----------------------------
21+
DROP TABLE IF EXISTS `tb_user`;
22+
CREATE TABLE `tb_user` (
23+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
24+
`name` varchar(255) DEFAULT NULL,
25+
`passwd` varchar(255) DEFAULT NULL,
26+
PRIMARY KEY (`id`)
27+
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
28+
29+
-- ----------------------------
30+
-- Records of tb_user
31+
-- ----------------------------
32+
INSERT INTO `tb_user` VALUES ('1', 'tom', '123456');
33+
INSERT INTO `tb_user` VALUES ('2', 'susan', '123456');
34+
INSERT INTO `tb_user` VALUES ('3', 'super', '565656');
35+
INSERT INTO `tb_user` VALUES ('4', 'about', '888888');
36+
INSERT INTO `tb_user` VALUES ('5', 'relation', '000000');
37+
INSERT INTO `tb_user` VALUES ('6', 'ship', '345678');
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.tosea;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import tk.mybatis.spring.annotation.MapperScan;
6+
7+
@SpringBootApplication
8+
@MapperScan("com.tosea.mapper")
9+
public class SpringbootMybatisApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringbootMybatisApplication.class, args);
13+
}
14+
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.tosea.mapper;
2+
3+
import com.tosea.pojo.User;
4+
import tk.mybatis.mapper.common.Mapper;
5+
6+
public interface UserMapper extends Mapper<User> {
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tosea.pojo;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import tk.mybatis.mapper.annotation.KeySql;
7+
8+
import javax.persistence.Id;
9+
import javax.persistence.Table;
10+
11+
@Data
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@Table(name = "tb_user")
15+
public class User {
16+
17+
@Id
18+
@KeySql(useGeneratedKeys = true)
19+
private Long id;
20+
private String name;
21+
private String passwd;
22+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.tosea.service;
2+
3+
import com.github.pagehelper.PageHelper;
4+
import com.github.pagehelper.PageInfo;
5+
import com.tosea.mapper.UserMapper;
6+
import com.tosea.pojo.User;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.util.CollectionUtils;
10+
import tk.mybatis.mapper.entity.Example;
11+
12+
import java.util.List;
13+
14+
@Service
15+
public class UserService {
16+
17+
@Autowired
18+
private UserMapper userMapper;
19+
20+
public void insert(User user) {
21+
int count = userMapper.insert(user);
22+
if (count != 1) {
23+
System.out.println("新增失败");
24+
}
25+
}
26+
27+
public void update(User user) {
28+
int count = userMapper.updateByPrimaryKey(user);
29+
if (count != 1) {
30+
System.out.println("更新失败");
31+
}
32+
}
33+
34+
public void delete(Long id) {
35+
int count = userMapper.deleteByPrimaryKey(id);
36+
if (count != 1) {
37+
System.out.println("删除失败");
38+
}
39+
}
40+
41+
public void get(Long id) {
42+
User user = userMapper.selectByPrimaryKey(id);
43+
if (user == null) {
44+
System.out.println("结果为空");
45+
} else {
46+
System.out.println(user);
47+
}
48+
}
49+
50+
public void list() {
51+
List<User> list = userMapper.selectAll();
52+
if (CollectionUtils.isEmpty(list)) {
53+
System.out.println("结果为空");
54+
} else {
55+
System.out.println(list);
56+
}
57+
}
58+
59+
public void page() {
60+
PageHelper.startPage(1, 3);
61+
List<User> list = userMapper.selectAll();
62+
PageInfo<User> info = new PageInfo<>(list);
63+
if (CollectionUtils.isEmpty(list)) {
64+
System.out.println("结果为空");
65+
} else {
66+
System.out.println(info);
67+
}
68+
}
69+
70+
public void pageByExample() {
71+
PageHelper.startPage(1, 3);
72+
Example example = new Example(User.class);
73+
// 当名字含有a
74+
example.createCriteria().orLike("name", "%a%").orEqualTo("letter");
75+
List<User> list = userMapper.selectByExample(example);
76+
PageInfo<User> info = new PageInfo<>(list);
77+
if (CollectionUtils.isEmpty(list)) {
78+
System.out.println("结果为空");
79+
} else {
80+
System.out.println(info);
81+
}
82+
}
83+
84+
/*public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) throws LyException {
85+
// 分页
86+
PageHelper.startPage(page, rows);
87+
Example example = new Example(Brand.class);
88+
// 过滤
89+
if (StringUtils.isNotBlank(key)) {
90+
example.createCriteria().orLike("name", "%" + key + "%").orEqualTo("letter", key.toUpperCase()); // 设立条件
91+
}
92+
// 排序
93+
if (StringUtils.isNotBlank(sortBy)) {
94+
String orderByClause = sortBy + (desc ? " DESC" : " ASC");
95+
example.setOrderByClause(orderByClause);
96+
}
97+
// 查询
98+
List<Brand> list = brandMapper.selectByExample(example);
99+
if (CollectionUtils.isEmpty(list)) {
100+
throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
101+
}
102+
// 解析分页结果
103+
PageInfo<Brand> info = new PageInfo<>(list);
104+
105+
return new PageResult<>(info.getTotal(), list);
106+
}*/
107+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring:
2+
application:
3+
name: item-service
4+
datasource:
5+
url: jdbc:mysql://localhost:3306/springboot-learn
6+
username: root
7+
password: 123456
8+
hikari:
9+
maximum-pool-size: 30
10+
minimum-idle: 10
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.tosea;
2+
3+
import com.tosea.pojo.User;
4+
import com.tosea.service.UserService;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
@RunWith(SpringRunner.class)
12+
@SpringBootTest
13+
public class SpringbootMybatisApplicationTests {
14+
15+
@Test
16+
public void contextLoads() {
17+
}
18+
19+
@Autowired
20+
private UserService userService;
21+
22+
/**
23+
* 新增
24+
*/
25+
@Test
26+
public void insert() {
27+
User user = new User();
28+
user.setName("tosea");
29+
user.setPasswd("123456");
30+
userService.insert(user);
31+
}
32+
33+
/**
34+
* 更新
35+
*/
36+
@Test
37+
public void update() {
38+
User user = new User();
39+
user.setId(1L);
40+
user.setName("aesot");
41+
user.setPasswd("654321");
42+
userService.update(user);
43+
}
44+
45+
/**
46+
* 删除
47+
*/
48+
@Test
49+
public void delete() {
50+
userService.delete(2L);
51+
}
52+
53+
/**
54+
* 查询单个
55+
*/
56+
@Test
57+
public void get() {
58+
userService.get(1L);
59+
}
60+
61+
/**
62+
* 查询所有
63+
*/
64+
@Test
65+
public void list() {
66+
userService.list();
67+
}
68+
69+
/**
70+
* 分页查询
71+
*/
72+
@Test
73+
public void page() {
74+
userService.page();
75+
}
76+
77+
/**
78+
* 分页查询 + 条件过滤
79+
*/
80+
@Test
81+
public void pageByExample() {
82+
userService.pageByExample();
83+
}
84+
85+
}

0 commit comments

Comments
 (0)