Skip to content

Commit 2bd299f

Browse files
committed
添加mybatis
1 parent dfbacbc commit 2bd299f

20 files changed

Lines changed: 837 additions & 1 deletion

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ java后端项目练手:kissing_closed_eyes:
55
- [x] 简单的手写aop
66
- [x] 简单的手写线程池
77
- [x] 微信支付demo
8-
8+
- [x] Mybatis测试

mybatis-test/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Mybatis-test
2+
一个mybatis测试
3+
4+
Mybatis入门:https://blog.csdn.net/lemon_TT/article/details/114846109
5+
6+
Mybatis源码分析:https://blog.csdn.net/lemon_TT/article/details/123223136

mybatis-test/pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.demo</groupId>
8+
<artifactId>mybatis</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>mybatis</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.11</version>
21+
<scope>test</scope>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.mybatis</groupId>
26+
<artifactId>mybatis</artifactId>
27+
<version>3.4.1</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>mysql</groupId>
32+
<artifactId>mysql-connector-java</artifactId>
33+
<version>8.0.16</version>
34+
</dependency>
35+
36+
</dependencies>
37+
38+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.demo;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.demo.mapper;
2+
3+
import org.demo.po.Department;
4+
5+
public interface DepartmentMapper {
6+
7+
Department MyDept(Integer id);
8+
9+
Department getDeptStep(Integer id);
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.demo.mapper;
2+
3+
import org.demo.po.Employee;
4+
5+
import java.util.List;
6+
7+
public interface DynamicSQLMapper {
8+
9+
List<Employee> getEmpsByConditionIf(Employee employee);
10+
11+
List<Employee> getEmpsByConditionIfWithWhere(Employee employee);
12+
13+
Integer updateEmp(Employee employee);
14+
15+
List<Employee> selectEmployeeBySql();
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.demo.mapper;
2+
3+
import org.apache.ibatis.annotations.Options;
4+
import org.apache.ibatis.annotations.Param;
5+
import org.apache.ibatis.annotations.Select;
6+
import org.demo.po.Employee;
7+
8+
import java.util.List;
9+
10+
/**
11+
* @author Shawn
12+
* @version 1.0
13+
* @description: TODO
14+
* @date 2022/2/18 11:16
15+
*/
16+
public interface EmployeeMapper {
17+
18+
19+
Employee getEmpAndDepart(Integer id);
20+
21+
List<Employee> getEmpAndDepart1();
22+
23+
List<Employee> getEmpAndDepart2();
24+
25+
@Select("select * from employee where id = #{id}")
26+
@Options
27+
Employee getEmpById(@Param("id") Integer id);
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.demo.mapper;
2+
3+
import org.demo.po.Employee;
4+
5+
import java.util.List;
6+
7+
/**
8+
* @author Shawn
9+
* @version 1.0
10+
* @description: TODO
11+
* @date 2022/2/18 11:16
12+
*/
13+
public interface EmployeeMapper1 {
14+
15+
16+
List<Employee> getEmpsWithDiscriminator();
17+
18+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.demo.plugin;
2+
3+
import org.apache.ibatis.executor.Executor;
4+
import org.apache.ibatis.mapping.MappedStatement;
5+
import org.apache.ibatis.plugin.*;
6+
7+
import java.lang.reflect.Method;
8+
import java.util.Properties;
9+
10+
/**
11+
* @author Shawn
12+
* @version 1.0
13+
* @description: TODO
14+
* @date 2022/2/28 21:22
15+
*/
16+
@Intercepts({@Signature(
17+
type= Executor.class,
18+
method = "update",
19+
args = {MappedStatement.class,Object.class})})
20+
public class ExamplePlugin implements Interceptor {
21+
//自定义属性
22+
private int number;
23+
24+
// 当执行目标方法时会被方法拦截
25+
public Object intercept(Invocation invocation) throws Throwable {
26+
Object target = invocation.getTarget(); //被代理对象
27+
Method method = invocation.getMethod(); //代理方法
28+
Object[] args = invocation.getArgs(); //方法参数
29+
// do something ...... 方法拦截前执行代码块
30+
Object result = invocation.proceed();
31+
// do something .......方法拦截后执行代码块
32+
return result;
33+
}
34+
// 生成代理对象,可自定义生成代理对象,这样就无需配置@Intercepts注解。另外需要自行判断是否为拦截目标接口。
35+
public Object plugin(Object target) {
36+
return Plugin.wrap(target,this);// 调用通用插件代理生成机器
37+
}
38+
39+
//配置属性
40+
public void setProperties(Properties properties) {
41+
this.number = Integer.parseInt(properties.getProperty("number", String.valueOf(100)));
42+
}
43+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.demo.po;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author Shawn
7+
* @version 1.0
8+
* @description: TODO
9+
* @date 2022/3/3 11:51
10+
*/
11+
public class Department {
12+
private Integer id;
13+
private String departmentName;
14+
private List<Employee> emps;
15+
16+
@Override
17+
public String toString() {
18+
return "Department{" +
19+
"id=" + id +
20+
", departmentName='" + departmentName + '\'' +
21+
", emps=" + emps +
22+
'}';
23+
}
24+
25+
public Integer getId() {
26+
return id;
27+
}
28+
29+
public void setId(Integer id) {
30+
this.id = id;
31+
}
32+
33+
public String getDepartmentName() {
34+
return departmentName;
35+
}
36+
37+
public void setDepartmentName(String departmentName) {
38+
this.departmentName = departmentName;
39+
}
40+
41+
public List<Employee> getEmps() {
42+
return emps;
43+
}
44+
45+
public void setEmps(List<Employee> emps) {
46+
this.emps = emps;
47+
}
48+
}

0 commit comments

Comments
 (0)