├── .gitignore ├── README.md ├── database ├── task.sql └── user.sql ├── pic └── book_info.jpg ├── 示例1 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── DemoApplication.java │ │ │ └── model │ │ │ └── User.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── DemoApplicationTests.java ├── 示例10 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ └── User.java │ └── resources │ ├── application.properties │ └── info.xml ├── 示例11 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── DemoApplication.java │ │ │ └── model │ │ │ ├── Bag.java │ │ │ ├── Book.java │ │ │ ├── Boy.java │ │ │ ├── Girl.java │ │ │ └── User.java │ └── resources │ │ ├── application.properties │ │ ├── com │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ └── UserDao.xml │ │ └── mybatis-config.xml │ └── test │ └── java │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── DemoApplicationTests.java ├── 示例12 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ ├── builder │ │ ├── GardenSchoolUserBuilder.java │ │ ├── SunnySchoolUserBuilder.java │ │ └── UserBuilder.java │ │ └── model │ │ └── User.java │ └── resources │ └── application.properties ├── 示例13 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── DemoApplication.java │ │ │ └── model │ │ │ ├── Boy.java │ │ │ ├── Girl.java │ │ │ └── User.java │ └── resources │ │ ├── application.properties │ │ ├── com │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ └── UserDao.xml │ │ └── mybatis-config.xml │ └── test │ └── java │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── DemoApplicationTests.java ├── 示例14 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── DemoApplication.java │ │ │ ├── controller │ │ │ └── UserController.java │ │ │ ├── dao │ │ │ └── UserDao.java │ │ │ ├── model │ │ │ └── User.java │ │ │ └── provider │ │ │ └── UserProvider.java │ └── resources │ │ ├── application.properties │ │ └── mybatis-config.xml │ └── test │ └── java │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── DemoApplicationTests.java ├── 示例15 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ └── model │ │ └── User.java │ └── resources │ └── application.properties ├── 示例16 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ └── model │ │ └── User.java │ └── resources │ ├── application.properties │ ├── demoScript.sql │ └── mybatis-config.xml ├── 示例17 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ └── User.java │ └── resources │ └── application.properties ├── 示例18 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ ├── UserMapper.java │ │ └── model │ │ └── User.java │ └── resources │ ├── application.properties │ ├── com │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── UserMapper.xml │ └── mybatis-config.xml ├── 示例19 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ └── model │ │ ├── ProxyHandler.java │ │ └── User.java │ └── resources │ └── application.properties ├── 示例2 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── MainController.java │ │ │ ├── MybatisdemoApplication.java │ │ │ ├── User.java │ │ │ └── UserMapper.java │ └── resources │ │ ├── application.properties │ │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── UserMapper.xml │ └── test │ └── java │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── MybatisdemoApplicationTests.java ├── 示例20 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── DemoApplication.java │ └── resources │ └── application.properties ├── 示例21 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ └── model │ │ ├── UserModel01.java │ │ ├── UserModel02.java │ │ ├── UserModel03.java │ │ ├── UserModel04.java │ │ └── UserModel05.java │ └── resources │ └── application.properties ├── 示例22 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── DemoApplication.java │ └── resources │ └── application.properties ├── 示例23 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ └── model │ │ └── User.java │ └── resources │ ├── application.properties │ ├── com │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── UserDao.xml │ └── mybatis-config.xml ├── 示例24 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ └── model │ │ ├── Task.java │ │ └── User.java │ └── resources │ ├── application.properties │ ├── com │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── UserDao.xml │ └── mybatis-config.xml ├── 示例25 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── DemoApplication.java │ │ │ └── model │ │ │ └── User.java │ └── resources │ │ ├── application.properties │ │ ├── com │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ └── UserMapper.xml │ │ └── mybatis-config.xml │ └── test │ └── java │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── DemoApplicationTests.java ├── 示例26 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── DemoApplication.java │ │ │ └── model │ │ │ ├── Task.java │ │ │ └── User.java │ └── resources │ │ ├── application.properties │ │ ├── com │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── Dao01.xml │ │ │ └── Dao02.xml │ │ └── mybatis-config.xml │ └── test │ └── java │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── DemoApplicationTests.java ├── 示例27 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ ├── model │ │ └── Performer.java │ │ └── processor │ │ ├── ContactOfficer.java │ │ ├── Handler.java │ │ ├── MailSender.java │ │ └── MaterialManager.java │ └── resources │ └── application.properties ├── 示例28 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ ├── model │ │ └── User.java │ │ └── plugin │ │ └── YeecodeInterceptor.java │ └── resources │ ├── application.properties │ ├── com │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── UserMapper.xml │ └── mybatis-config.xml ├── 示例29 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── DemoApplication.java │ └── resources │ └── application.properties ├── 示例3 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ ├── User.java │ │ └── UserMapper.java │ └── resources │ ├── application.properties │ ├── com │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── UserMapper.xml │ └── mybatis-config.xml ├── 示例4 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ ├── Phone.java │ │ ├── PhoneMessageDecorator.java │ │ ├── PhoneRecordDecorator.java │ │ └── TelePhone.java │ └── resources │ └── application.properties ├── 示例5 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── Book.java │ │ ├── DemoApplication.java │ │ └── User.java │ └── resources │ ├── application.properties │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── UserMapper.xml ├── 示例6 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ ├── Student.java │ │ ├── User.java │ │ └── UserMapper.java │ └── resources │ ├── application.properties │ ├── com │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ └── UserMapper.xml │ └── mybatis-config.xml ├── 示例7 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── DemoApplication.java │ │ │ └── model │ │ │ ├── Cleaning.java │ │ │ ├── WipeBlackboard.java │ │ │ └── WipeGlass.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── DemoApplicationTests.java ├── 示例8 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── yeecode │ │ │ └── mybatisdemo │ │ │ ├── DemoApplication.java │ │ │ └── model │ │ │ ├── User.java │ │ │ ├── UserInterface.java │ │ │ └── UserProxy.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── github │ └── yeecode │ └── mybatisdemo │ └── DemoApplicationTests.java └── 示例9 ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── github │ │ └── yeecode │ │ └── mybatisdemo │ │ ├── DemoApplication.java │ │ └── model │ │ ├── ProxyHandler.java │ │ ├── User.java │ │ └── UserInterface.java └── resources │ └── application.properties └── test └── java └── com └── github └── yeecode └── mybatisdemo └── DemoApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vscode 3 | .settings 4 | .mvn 5 | .classpath 6 | .project 7 | target 8 | out 9 | *.iml 10 | *.rar 11 | *.7z 12 | *.tempdata 13 | -------------------------------------------------------------------------------- /database/task.sql: -------------------------------------------------------------------------------- 1 | -- ---------------------------- 2 | -- Table structure for task 3 | -- ---------------------------- 4 | DROP TABLE IF EXISTS `task`; 5 | CREATE TABLE `task` ( 6 | `id` int(11) NOT NULL AUTO_INCREMENT, 7 | `userId` int(11) NOT NULL, 8 | `taskName` varchar(255) NOT NULL, 9 | PRIMARY KEY (`id`) 10 | ) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8mb4; 11 | 12 | -- ---------------------------- 13 | -- Records of task 14 | -- ---------------------------- 15 | INSERT INTO `task` VALUES ('1', '1', 'Clean classroom.'); 16 | INSERT INTO `task` VALUES ('2', '1', 'Open the door.'); 17 | INSERT INTO `task` VALUES ('3', '2', 'Open windows.'); 18 | INSERT INTO `task` VALUES ('4', '3', 'Clean the blackboard.'); 19 | INSERT INTO `task` VALUES ('5', '2', 'Buy some boos.'); 20 | INSERT INTO `task` VALUES ('6', '3', 'Buy some pens.'); 21 | INSERT INTO `task` VALUES ('7', '4', 'Buy some flowers.'); -------------------------------------------------------------------------------- /database/user.sql: -------------------------------------------------------------------------------- 1 | -- ---------------------------- 2 | -- Table structure for user 3 | -- ---------------------------- 4 | DROP TABLE IF EXISTS `user`; 5 | CREATE TABLE `user` ( 6 | `id` int(11) NOT NULL AUTO_INCREMENT, 7 | `name` varchar(255) NOT NULL, 8 | `email` varchar(255) DEFAULT NULL, 9 | `age` int(11) DEFAULT NULL, 10 | `sex` int(255) DEFAULT NULL, 11 | `schoolName` varchar(255) DEFAULT NULL, 12 | PRIMARY KEY (`id`) 13 | ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4; 14 | 15 | -- ---------------------------- 16 | -- Records of user 17 | -- ---------------------------- 18 | INSERT INTO `user` VALUES ('1', '易哥', 'yeecode@sample.com', '18', '0', 'Sunny School'); 19 | INSERT INTO `user` VALUES ('2', '莉莉', 'lili@sample.com', '15', '1', 'Garden School'); 20 | INSERT INTO `user` VALUES ('3', '杰克', 'jack@sample.com', '25', '0', 'Sunny School'); 21 | INSERT INTO `user` VALUES ('4', '张大壮', 'zdazhaung@sample.com', '16', '0', 'Garden School'); 22 | INSERT INTO `user` VALUES ('5', '王小壮', 'wxiaozhuang@sample.com', '27', '0', 'Sunny School'); 23 | INSERT INTO `user` VALUES ('6', '露西', 'lucy@sample.com', '14', '1', 'Garden School'); 24 | INSERT INTO `user` VALUES ('7', '李二壮', 'lerzhuang@sample.com', '9', '0', 'Sunny School'); 25 | -------------------------------------------------------------------------------- /pic/book_info.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yeecode/MyBatisDemo/3860e2fa842214dcf252af15b6ca94a644d9fc8e/pic/book_info.jpg -------------------------------------------------------------------------------- /示例1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | mysql 35 | mysql-connector-java 36 | runtime 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /示例1/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | import java.sql.Connection; 7 | import java.sql.DriverManager; 8 | import java.sql.ResultSet; 9 | import java.sql.Statement; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | @SpringBootApplication 14 | public class DemoApplication { 15 | public static void main(String[] args) throws Exception { 16 | String url = "jdbc:mysql://127.0.0.1:3306/yeecode?serverTimezone=UTC"; 17 | String userName = "root"; 18 | String password = "yeecode"; 19 | 20 | User userParam = new User(); 21 | userParam.setSchoolName("Sunny School"); 22 | 23 | // 第一步:加载驱动程序 24 | Class.forName("com.mysql.jdbc.Driver"); 25 | 26 | // 第二步:获得数据库的连接 27 | Connection conn = DriverManager.getConnection(url, userName, password); 28 | 29 | // 第三步:创建语句并执行 30 | Statement stmt = conn.createStatement(); 31 | ResultSet resultSet = stmt.executeQuery("SELECT * FROM `user` WHERE schoolName = \'" + userParam.getSchoolName() + "\';"); 32 | 33 | // 第四步:处理数据库操作结果 34 | List userList = new ArrayList<>(); 35 | while(resultSet.next()){ 36 | User user = new User(); 37 | user.setId(resultSet.getInt("id")); 38 | user.setName(resultSet.getString("name")); 39 | user.setEmail(resultSet.getString("email")); 40 | user.setAge(resultSet.getInt("age")); 41 | user.setSex(resultSet.getInt("sex")); 42 | user.setSchoolName(resultSet.getString("schoolName")); 43 | userList.add(user); 44 | } 45 | 46 | // 第五步:关闭连接 47 | stmt.close(); 48 | 49 | for (User user : userList) { 50 | System.out.println("name : " + user.getName() + " ; email : " + user.getEmail()); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /示例1/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class User { 4 | private Integer id; 5 | private String name; 6 | private String email; 7 | private Integer age; 8 | private Integer sex; 9 | private String schoolName; 10 | 11 | public User() { 12 | } 13 | 14 | public Integer getId() { 15 | return id; 16 | } 17 | 18 | public void setId(Integer id) { 19 | this.id = id; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | public String getEmail() { 31 | return email; 32 | } 33 | 34 | public void setEmail(String email) { 35 | this.email = email; 36 | } 37 | 38 | public Integer getAge() { 39 | return age; 40 | } 41 | 42 | public void setAge(Integer age) { 43 | this.age = age; 44 | } 45 | 46 | public Integer getSex() { 47 | return sex; 48 | } 49 | 50 | public void setSex(Integer sex) { 51 | this.sex = sex; 52 | } 53 | 54 | public String getSchoolName() { 55 | return schoolName; 56 | } 57 | 58 | public void setSchoolName(String schoolName) { 59 | this.schoolName = schoolName; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /示例1/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例1/src/test/java/com/github/yeecode/mybatisdemo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /示例10/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | ognl 29 | ognl 30 | 3.1.24 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /示例10/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.w3c.dom.Document; 5 | 6 | import javax.xml.parsers.DocumentBuilder; 7 | import javax.xml.parsers.DocumentBuilderFactory; 8 | import javax.xml.xpath.XPath; 9 | import javax.xml.xpath.XPathExpression; 10 | import javax.xml.xpath.XPathFactory; 11 | 12 | @SpringBootApplication 13 | public class DemoApplication { 14 | public static void main(String[] args) { 15 | try { 16 | String resource = "info.xml"; 17 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 18 | DocumentBuilder db = dbf.newDocumentBuilder(); 19 | Document doc = db.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream(resource)); 20 | 21 | XPathFactory factory = XPathFactory.newInstance(); 22 | XPath xpath = factory.newXPath(); 23 | 24 | XPathExpression compile = xpath.compile("/members/user[id=1]"); 25 | System.out.println(compile.evaluate(doc)); 26 | } catch (Exception ex) { 27 | ex.printStackTrace(); 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /示例10/src/main/java/com/github/yeecode/mybatisdemo/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class User { 4 | private Integer id; 5 | private String name; 6 | private String school; 7 | 8 | public Integer getId() { 9 | return id; 10 | } 11 | 12 | public void setId(Integer id) { 13 | this.id = id; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | 24 | public String getSchool() { 25 | return school; 26 | } 27 | 28 | public void setSchool(String school) { 29 | this.school = school; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /示例10/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例10/src/main/resources/info.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1 5 | 易哥 6 | Sunny School 7 | 8 | 9 | 2 10 | 莉莉 11 | Garden School 12 | 13 | -------------------------------------------------------------------------------- /示例11/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo03 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | mysql 36 | mysql-connector-java 37 | 8.0.17 38 | 39 | 40 | 41 | 42 | org.mybatis 43 | mybatis 44 | 3.5.2 45 | 46 | 47 | 48 | com.github.pagehelper 49 | pagehelper 50 | 5.1.10 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | org.springframework.boot 60 | spring-boot-maven-plugin 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /示例11/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import org.apache.ibatis.io.Resources; 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.util.List; 13 | 14 | @SpringBootApplication 15 | public class DemoApplication { 16 | public static void main(String[] args) { 17 | System.out.println("Demo03 is a demo for configuration files, it can't work."); 18 | String resource = "mybatis-config.xml"; 19 | InputStream inputStream = null; 20 | try { 21 | inputStream = Resources.getResourceAsStream(resource); 22 | } catch (IOException e) { 23 | e.printStackTrace(); 24 | } 25 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 26 | 27 | try (SqlSession session = sqlSessionFactory.openSession()) { 28 | User userBean = session.selectOne("com.github.yeecode.mybatisdemo.dao.UserDao.selectUser_A", 1); 29 | System.out.println(userBean.toString()); 30 | userBean = session.selectOne("com.github.yeecode.mybatisdemo.dao.UserDao.selectUser_B", 1); 31 | System.out.println(userBean.toString()); 32 | userBean = session.selectOne("com.github.yeecode.mybatisdemo.dao.UserDao.selectUser_B", 2); 33 | System.out.println(userBean.toString()); 34 | 35 | List userList = session.selectList("com.github.yeecode.mybatisdemo.dao.UserDao.selectUsers", new int[]{1, 2}); 36 | for (User user : userList) { 37 | System.out.println(user.toString()); 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /示例11/src/main/java/com/github/yeecode/mybatisdemo/model/Bag.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class Bag { 4 | private String name; 5 | private Integer weight; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | 15 | public Integer getWeight() { 16 | return weight; 17 | } 18 | 19 | public void setWeight(Integer weight) { 20 | this.weight = weight; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /示例11/src/main/java/com/github/yeecode/mybatisdemo/model/Book.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class Book { 4 | private String name; 5 | private Integer price; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | 15 | public Integer getPrice() { 16 | return price; 17 | } 18 | 19 | public void setPrice(Integer price) { 20 | this.price = price; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /示例11/src/main/java/com/github/yeecode/mybatisdemo/model/Boy.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class Boy extends User { 4 | private String age; 5 | 6 | public String getAge() { 7 | return age; 8 | } 9 | 10 | public void setAge(String age) { 11 | this.age = age; 12 | } 13 | 14 | @Override 15 | public String toString() { 16 | 17 | return super.toString() +"\n" 18 | +"Boy{" + 19 | "age='" + age + '\'' + 20 | '}'; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /示例11/src/main/java/com/github/yeecode/mybatisdemo/model/Girl.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class Girl extends User { 4 | private String email; 5 | 6 | public String getEmail() { 7 | return email; 8 | } 9 | 10 | public void setEmail(String email) { 11 | this.email = email; 12 | } 13 | 14 | @Override 15 | public String toString() { 16 | return super.toString() + "\n" 17 | +"Girl{" + 18 | "email=" + email + 19 | '}'; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /示例11/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | public class User implements Serializable { 7 | private Integer id; 8 | private String name; 9 | private Integer sex; 10 | private String schoolName; 11 | private Bag bag; 12 | private List books; 13 | 14 | public User() { 15 | } 16 | 17 | public User(Integer id, String name, Integer sex, String schoolName) { 18 | this.id = id; 19 | this.name = name; 20 | this.sex = sex; 21 | this.schoolName = schoolName; 22 | } 23 | 24 | public Integer getId() { 25 | return id; 26 | } 27 | 28 | public void setId(Integer id) { 29 | this.id = id; 30 | } 31 | 32 | public String getName() { 33 | return name; 34 | } 35 | 36 | public void setName(String name) { 37 | this.name = name; 38 | } 39 | 40 | public Integer getSex() { 41 | return sex; 42 | } 43 | 44 | public void setSex(Integer sex) { 45 | this.sex = sex; 46 | } 47 | 48 | public String getSchoolName() { 49 | return schoolName; 50 | } 51 | 52 | public void setSchoolName(String schoolName) { 53 | this.schoolName = schoolName; 54 | } 55 | 56 | public Bag getBag() { 57 | return bag; 58 | } 59 | 60 | public void setBag(Bag bag) { 61 | this.bag = bag; 62 | } 63 | 64 | public List getBooks() { 65 | return books; 66 | } 67 | 68 | public void setBooks(List books) { 69 | this.books = books; 70 | } 71 | 72 | @Override 73 | public String toString() { 74 | return "User{" + 75 | "id=" + id + 76 | ", name='" + name + '\'' + 77 | ", sex=" + sex + 78 | ", schoolName='" + schoolName + '\'' + 79 | '}'; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /示例11/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例11/src/main/resources/com/github/yeecode/mybatisdemo/UserDao.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | WHERE `id` = #{id}; 54 | 55 | 56 | 59 | 60 | 63 | 64 | 72 | 73 | 74 | DELETE FROM `user` 75 | 76 | 77 | 78 | 79 | 80 | SELECT LAST_INSERT_ID() 81 | 82 | INSERT INTO `user` VALUES (#{id},#{name},#{email},#{age},#{sex},#{schoolName}); 83 | 84 | 85 | 86 | UPDATE `user` 87 | SET `email` = #{email} 88 | 89 | 90 | -------------------------------------------------------------------------------- /示例11/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /示例11/src/test/java/com/github/yeecode/mybatisdemo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /示例12/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /示例12/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.builder.GardenSchoolUserBuilder; 4 | import com.github.yeecode.mybatisdemo.builder.SunnySchoolUserBuilder; 5 | import com.github.yeecode.mybatisdemo.builder.UserBuilder; 6 | import com.github.yeecode.mybatisdemo.model.User; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | 9 | @SpringBootApplication 10 | public class DemoApplication { 11 | public static void main(String[] args) { 12 | // 第一种:先创建对象在设置属性的方式建造对象 13 | User user01 = new User(); 14 | user01.setName("Candy"); 15 | user01.setEmail("candy@sunnyschool.com"); 16 | user01.setAge(7); 17 | user01.setSex(1); 18 | user01.setSchoolName("Sunny School"); 19 | // 第二种:用构造方法建造对象 20 | User user02 = new User("Candy", "candy@sunnyschool.com", 7, 1, "Sunny School"); 21 | 22 | // 第三种:用建造者建造对象 23 | 24 | // 3.1 用匿名建造者建造一个对象 25 | User user03 = new SunnySchoolUserBuilder("Candy").setSex(1).build(); 26 | 27 | // 3.2 分步设置建造者属性,建造一个对象 28 | UserBuilder userBuilder04 = new SunnySchoolUserBuilder("Eric"); 29 | userBuilder04.setEmail("supereric@abc.com").build(); 30 | User user04 = userBuilder04.build(); 31 | 32 | System.out.println(user01); 33 | System.out.println(user02); 34 | System.out.println(user03); 35 | System.out.println(user04); 36 | 37 | } 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /示例12/src/main/java/com/github/yeecode/mybatisdemo/builder/GardenSchoolUserBuilder.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.builder; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | 5 | public class GardenSchoolUserBuilder implements UserBuilder { 6 | private String name; 7 | private String email; 8 | private Integer age; 9 | private Integer sex; 10 | private String schoolName; 11 | 12 | public GardenSchoolUserBuilder(String name) { 13 | this.name = name; 14 | } 15 | 16 | public GardenSchoolUserBuilder setEmail(String email) { 17 | this.email = email; 18 | return this; 19 | } 20 | 21 | public GardenSchoolUserBuilder setAge(Integer age) { 22 | this.age = age; 23 | return this; 24 | } 25 | 26 | public GardenSchoolUserBuilder setSex(Integer sex) { 27 | this.sex = sex; 28 | return this; 29 | } 30 | 31 | public User build() { 32 | if (this.email == null) { 33 | this.email = this.name.toLowerCase().replace(" ", "").concat("@gardenschool.com"); 34 | } 35 | if (this.age == null) { 36 | this.age = 13; 37 | } 38 | if (this.sex == null) { 39 | this.sex = 0; 40 | } 41 | this.schoolName = "Garden School"; 42 | return new User(name, email, age, sex, schoolName); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /示例12/src/main/java/com/github/yeecode/mybatisdemo/builder/SunnySchoolUserBuilder.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.builder; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | 5 | public class SunnySchoolUserBuilder implements UserBuilder { 6 | private String name; 7 | private String email; 8 | private Integer age; 9 | private Integer sex; 10 | private String schoolName; 11 | 12 | public SunnySchoolUserBuilder(String name) { 13 | this.name = name; 14 | } 15 | 16 | public SunnySchoolUserBuilder setEmail(String email) { 17 | this.email = email; 18 | return this; 19 | } 20 | 21 | public SunnySchoolUserBuilder setAge(Integer age) { 22 | this.age = age; 23 | return this; 24 | } 25 | 26 | public SunnySchoolUserBuilder setSex(Integer sex) { 27 | this.sex = sex; 28 | return this; 29 | } 30 | 31 | public User build() { 32 | if (this.name != null && this.email == null) { 33 | this.email = this.name.toLowerCase().replace(" ", "").concat("@sunnyschool.com"); 34 | } 35 | if (this.age == null) { 36 | this.age = 7; 37 | } 38 | if (this.sex == null) { 39 | this.sex = 0; 40 | } 41 | this.schoolName = "Sunny School"; 42 | return new User(name, email, age, sex, schoolName); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /示例12/src/main/java/com/github/yeecode/mybatisdemo/builder/UserBuilder.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.builder; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | 5 | public interface UserBuilder { 6 | 7 | public UserBuilder setEmail(String email); 8 | 9 | public UserBuilder setAge(Integer age); 10 | 11 | public UserBuilder setSex(Integer sex); 12 | 13 | public User build(); 14 | } 15 | -------------------------------------------------------------------------------- /示例12/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class User { 4 | private String name; 5 | private String email; 6 | private Integer age; 7 | private Integer sex; 8 | private String schoolName; 9 | 10 | public User() { 11 | } 12 | 13 | public User(String name, String email, Integer age, Integer sex, String schoolName) { 14 | this.name = name; 15 | this.email = email; 16 | this.age = age; 17 | this.sex = sex; 18 | this.schoolName = schoolName; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getEmail() { 30 | return email; 31 | } 32 | 33 | public void setEmail(String email) { 34 | this.email = email; 35 | } 36 | 37 | public Integer getAge() { 38 | return age; 39 | } 40 | 41 | public void setAge(Integer age) { 42 | this.age = age; 43 | } 44 | 45 | public Integer getSex() { 46 | return sex; 47 | } 48 | 49 | public void setSex(Integer sex) { 50 | this.sex = sex; 51 | } 52 | 53 | public String getSchoolName() { 54 | return schoolName; 55 | } 56 | 57 | public void setSchoolName(String schoolName) { 58 | this.schoolName = schoolName; 59 | } 60 | 61 | public String toString() { 62 | return "User{name=".concat(name) 63 | .concat(", email =").concat(email) 64 | .concat(", age =").concat(age.toString()) 65 | .concat(", sex =").concat(sex.toString()) 66 | .concat(", schoolName =").concat(schoolName) 67 | .concat("}").concat("\n") ; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /示例12/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例13/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | mysql 36 | mysql-connector-java 37 | 8.0.17 38 | 39 | 40 | 41 | 42 | org.mybatis 43 | mybatis 44 | 3.5.2 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-maven-plugin 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /示例13/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import org.apache.ibatis.io.Resources; 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.util.List; 13 | 14 | @SpringBootApplication 15 | public class DemoApplication { 16 | public static void main(String[] args) { 17 | String resource = "mybatis-config.xml"; 18 | InputStream inputStream = null; 19 | try { 20 | inputStream = Resources.getResourceAsStream(resource); 21 | } catch (IOException e) { 22 | e.printStackTrace(); 23 | } 24 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 25 | 26 | try (SqlSession session = sqlSessionFactory.openSession()) { 27 | User userBean = session.selectOne("com.github.yeecode.mybatisdemo.dao.UserDao.selectUser_A", 1); 28 | System.out.println("selectUser_A, param=1 : " + userBean.toString()); 29 | userBean = session.selectOne("com.github.yeecode.mybatisdemo.dao.UserDao.selectUser_B", 1); 30 | System.out.println("selectUser_B, param=1 : " + userBean.toString()); 31 | userBean = session.selectOne("com.github.yeecode.mybatisdemo.dao.UserDao.selectUser_B", 2); 32 | System.out.println("selectUser_B, param=2 : " + userBean.toString()); 33 | 34 | User userParam = new User(); 35 | userParam.setName("莉莉"); 36 | userParam.setSchoolName("Garden School"); 37 | userBean = session.selectOne("com.github.yeecode.mybatisdemo.dao.UserDao.selectUserByNameAndSchoolName", userParam); 38 | System.out.println("selectUsersByNameAndSchoolName, param=userParam : " + userBean.toString()); 39 | 40 | userParam.setName(null); 41 | List userList = session.selectList("com.github.yeecode.mybatisdemo.dao.UserDao.selectUsersByNameOrSchoolName", userParam); 42 | System.out.println("selectUsersByNameOrSchoolName: "); 43 | for (User user : userList) { 44 | System.out.println(user.toString()); 45 | } 46 | 47 | userList = session.selectList("com.github.yeecode.mybatisdemo.dao.UserDao.selectUsers", new int[]{1, 2}); 48 | System.out.println("selectUsers, param=1,2 : "); 49 | for (User user : userList) { 50 | System.out.println(user.toString()); 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /示例13/src/main/java/com/github/yeecode/mybatisdemo/model/Boy.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class Boy extends User { 4 | private String age; 5 | 6 | public String getAge() { 7 | return age; 8 | } 9 | 10 | public void setAge(String age) { 11 | this.age = age; 12 | } 13 | 14 | @Override 15 | public String toString() { 16 | 17 | return super.toString() +"\n" 18 | +"Boy{" + 19 | "age='" + age + '\'' + 20 | '}'; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /示例13/src/main/java/com/github/yeecode/mybatisdemo/model/Girl.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class Girl extends User { 4 | private String email; 5 | 6 | public String getEmail() { 7 | return email; 8 | } 9 | 10 | public void setEmail(String email) { 11 | this.email = email; 12 | } 13 | 14 | @Override 15 | public String toString() { 16 | return super.toString() + "\n" 17 | +"Girl{" + 18 | "email=" + email + 19 | '}'; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /示例13/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | private Integer id; 7 | private String name; 8 | private Integer sex; 9 | private String schoolName; 10 | 11 | public User() { 12 | } 13 | 14 | public User(Integer id, String name, Integer sex, String schoolName) { 15 | this.id = id; 16 | this.name = name; 17 | this.sex = sex; 18 | this.schoolName = schoolName; 19 | } 20 | 21 | public Integer getId() { 22 | return id; 23 | } 24 | 25 | public void setId(Integer id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | public Integer getSex() { 38 | return sex; 39 | } 40 | 41 | public void setSex(Integer sex) { 42 | this.sex = sex; 43 | } 44 | 45 | public String getSchoolName() { 46 | return schoolName; 47 | } 48 | 49 | public void setSchoolName(String schoolName) { 50 | this.schoolName = schoolName; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return "User{" + 56 | "id=" + id + 57 | ", name='" + name + '\'' + 58 | ", sex=" + sex + 59 | ", schoolName='" + schoolName + '\'' + 60 | '}'; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /示例13/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例13/src/main/resources/com/github/yeecode/mybatisdemo/UserDao.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 55 | 56 | 59 | 60 | 68 | 69 | 70 | AND `schoolName` = #{schoolName} 71 | 72 | 73 | 77 | 78 | 89 | 90 | 93 | 94 | 97 | 98 | -------------------------------------------------------------------------------- /示例13/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /示例13/src/test/java/com/github/yeecode/mybatisdemo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /示例14/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo04 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.mybatis.spring.boot 29 | mybatis-spring-boot-starter 30 | 2.0.0 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | mysql 47 | mysql-connector-java 48 | 8.0.17 49 | 50 | 51 | 52 | 53 | org.mybatis 54 | mybatis 55 | 3.5.2 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-maven-plugin 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /示例14/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DemoApplication { 8 | public static void main(String[] args) { 9 | SpringApplication.run(DemoApplication.class, args); 10 | System.out.println("Via http://127.0.0.1:8099/user/test/ to trigger this demo."); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /示例14/src/main/java/com/github/yeecode/mybatisdemo/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.controller; 2 | 3 | import com.github.yeecode.mybatisdemo.dao.UserDao; 4 | import com.github.yeecode.mybatisdemo.model.User; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | @RequestMapping("/user") 13 | public class UserController { 14 | @Autowired 15 | private UserDao userDao; 16 | 17 | @RequestMapping("/test") 18 | public String index() { 19 | 20 | User user = userDao.queryUserById(1); 21 | System.out.println("queryUserById(1):" + user.getName()); 22 | 23 | List userList = userDao.queryUsersByIds(new int[]{1,3}); 24 | System.out.print("queryUsersByIds(new Integer[]{1,3}):"); 25 | for (User item : userList) { 26 | System.out.print(item.getName() + "; "); 27 | } 28 | System.out.println(); 29 | 30 | userList = userDao.queryUsersBySchoolName("Sunny School"); 31 | System.out.print("queryUsersBySchoolName(\"Sunny School\") :"); 32 | 33 | for (User item : userList) { 34 | System.out.print(item.getName() + "; "); 35 | } 36 | System.out.println(); 37 | 38 | return "success"; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /示例14/src/main/java/com/github/yeecode/mybatisdemo/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.dao; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import com.github.yeecode.mybatisdemo.provider.UserProvider; 5 | import org.apache.ibatis.annotations.Mapper; 6 | import org.apache.ibatis.annotations.Select; 7 | import org.apache.ibatis.annotations.SelectProvider; 8 | 9 | import java.util.List; 10 | 11 | @Mapper 12 | public interface UserDao { 13 | @Select("SELECT * FROM `user` WHERE `id` = #{id}") 14 | User queryUserById(Integer id); 15 | 16 | @Select("") 24 | List queryUsersByIds(int[] ids); 25 | 26 | @SelectProvider(type = UserProvider.class, method = "queryUsersBySchoolName") 27 | List queryUsersBySchoolName(String schoolName); 28 | } 29 | -------------------------------------------------------------------------------- /示例14/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | private Integer id; 7 | private String name; 8 | private Integer sex; 9 | private String schoolName; 10 | 11 | public Integer getId() { 12 | return id; 13 | } 14 | 15 | public void setId(Integer id) { 16 | this.id = id; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public Integer getSex() { 28 | return sex; 29 | } 30 | 31 | public void setSex(Integer sex) { 32 | this.sex = sex; 33 | } 34 | 35 | public String getSchoolName() { 36 | return schoolName; 37 | } 38 | 39 | public void setSchoolName(String schoolName) { 40 | this.schoolName = schoolName; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "User{" + 46 | "id=" + id + 47 | ", name='" + name + '\'' + 48 | ", sex=" + sex + 49 | ", schoolName='" + schoolName + '\'' + 50 | '}'; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /示例14/src/main/java/com/github/yeecode/mybatisdemo/provider/UserProvider.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.provider; 2 | 3 | import org.apache.ibatis.jdbc.SQL; 4 | 5 | public class UserProvider { 6 | public String queryUsersBySchoolName() { 7 | return new SQL() 8 | .SELECT("*") 9 | .FROM("user") 10 | .WHERE("schoolName = #{schoolName}") 11 | .toString(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /示例14/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8099 2 | 3 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/yeecode?serverTimezone=UTC 4 | spring.datasource.username=root 5 | spring.datasource.password=yeecode 6 | 7 | -------------------------------------------------------------------------------- /示例14/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /示例14/src/test/java/com/github/yeecode/mybatisdemo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /示例15/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | ognl 35 | ognl 36 | 3.1.24 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /示例15/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import ognl.Ognl; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | import java.util.Date; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | @SpringBootApplication 12 | public class DemoApplication { 13 | public static void main(String[] args) { 14 | try { 15 | User user01 = new User(1, "易哥", 18); 16 | User user02 = new User(2, "莉莉", 15); 17 | Map userMap = new HashMap<>(); 18 | userMap.put("user1", user01); 19 | userMap.put("user2", user02); 20 | 21 | // Java方式读取列表中对象的属性值 22 | String userName = userMap.get("user2").getName(); 23 | System.out.println(userName); 24 | 25 | readAndWriteProps(userMap); 26 | callFunction(); 27 | runAfterParse(userMap); 28 | } catch (Exception ex) { 29 | ex.printStackTrace(); 30 | } 31 | 32 | } 33 | 34 | public static void readAndWriteProps(Map userMap) throws Exception { 35 | // 使用表达式读写根对象中信息的示例 36 | // 该示例中要用到的OGNL函数: 37 | // getValue(String expression, Object root) :对root内容执行expression中的操作,并返回结果 38 | 39 | // 读取根对象的属性值 40 | Integer age = (Integer) Ognl.getValue("age", userMap.get("user1")); 41 | System.out.println("读取根对象属性,得到age:" + age); 42 | // 设置根对象的属性值 43 | Ognl.getValue("age = 19", userMap.get("user1")); 44 | age = (Integer) Ognl.getValue("age", userMap.get("user1")); 45 | System.out.println("设置根对象属性后,得到age:" + age); 46 | 47 | // 使用表达式读写环境中信息的示例 48 | // 该示例中要用到的OGNL函数: 49 | // getValue(String expression, Map context, Object root) :在context环境中对root内容执行expression中的操作,并返回结果 50 | 51 | // 读取环境中的信息 52 | String userName2 = (String) Ognl.getValue("#user2.name", userMap, new Object()); 53 | System.out.println("读取环境中的信息,得到user2的name:" + userName2); 54 | // 读取环境中的信息,并进行判断 55 | Boolean result = (Boolean) Ognl.getValue("#user2.name != '丽丽'", userMap, new Object()); 56 | System.out.println("读取环境中的信息,并进行判断,得到:" + result); 57 | // 设置环境中的信息 58 | Ognl.getValue("#user2.name = '小华'", userMap, new Object()); 59 | String newUserName = (String) Ognl.getValue("#user2.name", userMap, new Object()); 60 | System.out.println("设置环境中的信息后,得到user2的name:" + newUserName); 61 | } 62 | 63 | public static void callFunction() throws Exception { 64 | // 调用对象方法 65 | Integer hashCode = (Integer) Ognl.getValue("hashCode()", "yeecode"); 66 | System.out.println("对字符串对象调用hashCode方法得到:" + hashCode); 67 | // 调用类方法 68 | Double result = (Double)Ognl.getValue("@java.lang.Math@random()", null); 69 | System.out.println("调用Math类中的静态方法random,得到:" + result); 70 | } 71 | 72 | public static void runAfterParse(Map userMap) throws Exception { 73 | String userName; 74 | 75 | // 先对表达式解析,然后再执行可以提高效率 76 | long time1 = new Date().getTime(); 77 | // 解析表达式 78 | Object expressionTree = Ognl.parseExpression("#user2.name"); 79 | // 重复运行多次 80 | for (int i = 0; i < 10000; i++) { 81 | userName = (String) Ognl.getValue(expressionTree, userMap, new Object()); 82 | } 83 | long time2 = new Date().getTime(); 84 | // 直接重复运行多次 85 | for (int i = 0; i < 10000; i++) { 86 | userName = (String) Ognl.getValue("#user2.name", userMap, new Object()); 87 | } 88 | long time3 = new Date().getTime(); 89 | 90 | System.out.println("编译之后再执行,共花费" + (time2 - time1) + "ms"); 91 | System.out.println("不编译直接执行,共花费" + (time3 - time2) + "ms"); 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /示例15/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class User { 4 | private Integer id; 5 | private String name; 6 | private String email; 7 | private Integer age; 8 | 9 | public User(Integer id, String name, Integer age) { 10 | this.id = id; 11 | this.name = name; 12 | this.age = age; 13 | } 14 | 15 | public static void saySlogan() { 16 | System.out.println("We are young!"); 17 | } 18 | 19 | public void sayHi() { 20 | System.out.println("Hi, I am " + this.name); 21 | } 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId(Integer id) { 28 | this.id = id; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public String getEmail() { 40 | return email; 41 | } 42 | 43 | public void setEmail(String email) { 44 | this.email = email; 45 | } 46 | 47 | public Integer getAge() { 48 | return age; 49 | } 50 | 51 | public void setAge(Integer age) { 52 | this.age = age; 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /示例15/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例16/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | mysql 28 | mysql-connector-java 29 | 8.0.17 30 | 31 | 32 | org.mybatis 33 | mybatis 34 | 3.5.2 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-maven-plugin 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /示例16/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.apache.ibatis.io.Resources; 4 | import org.apache.ibatis.jdbc.Null; 5 | import org.apache.ibatis.jdbc.ScriptRunner; 6 | import org.apache.ibatis.jdbc.SqlRunner; 7 | import org.apache.ibatis.session.SqlSession; 8 | import org.apache.ibatis.session.SqlSessionFactory; 9 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 | import org.springframework.boot.autoconfigure.SpringBootApplication; 11 | 12 | import java.io.IOException; 13 | import java.io.InputStream; 14 | import java.sql.Connection; 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | @SpringBootApplication 19 | public class DemoApplication { 20 | public static void main(String[] args) throws Exception { 21 | // 首先获取Connection对象备用,也可以使用其他方式获取 22 | String resource = "mybatis-config.xml"; 23 | InputStream inputStream = null; 24 | try { 25 | inputStream = Resources.getResourceAsStream(resource); 26 | } catch (IOException e) { 27 | e.printStackTrace(); 28 | } 29 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 30 | SqlSession sqlSession = sqlSessionFactory.openSession(); 31 | Connection connection = sqlSession.getConnection(); 32 | 33 | // SqlRunner类的使用 34 | String sql = "SELECT * FROM user WHERE age = ?;"; 35 | SqlRunner sqlRunner = new SqlRunner(connection); 36 | List> result = sqlRunner.selectAll(sql,15); 37 | System.out.println(result); 38 | 39 | // SqlRunner类的使用,email变量值为null 40 | sql = "UPDATE user SET email = ? WHERE id = 2;"; 41 | Integer out = sqlRunner.update(sql,Null.STRING); 42 | System.out.println(out); 43 | 44 | // ScriptRunner类的使用 45 | ScriptRunner scriptRunner = new ScriptRunner(connection); 46 | scriptRunner.runScript(Resources.getResourceAsReader("demoScript.sql")); 47 | 48 | connection.close(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /示例16/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | // 外部类 4 | public class User { 5 | private String name; 6 | private Bag bag = new Bag(); 7 | 8 | // 成员内部类 9 | public class Shoes{ 10 | private Boolean size; 11 | 12 | public Boolean getSize() { 13 | return size; 14 | } 15 | 16 | public void setSize(Boolean size) { 17 | this.size = size; 18 | } 19 | } 20 | 21 | // 静态内部类 22 | public static class Bag { 23 | private Integer width; 24 | private Integer length; 25 | 26 | public Integer getWidth() { 27 | return width; 28 | } 29 | 30 | public void setWidth(Integer width) { 31 | this.width = width; 32 | } 33 | 34 | public Integer getLength() { 35 | return length; 36 | } 37 | 38 | public void setLength(Integer length) { 39 | this.length = length; 40 | } 41 | } 42 | 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public void setName(String name) { 48 | this.name = name; 49 | } 50 | 51 | public Bag getBag() { 52 | return bag; 53 | } 54 | 55 | public void setBag(Bag bag) { 56 | this.bag = bag; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /示例16/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例16/src/main/resources/demoScript.sql: -------------------------------------------------------------------------------- 1 | UPDATE user SET email = 'yeecode@abc.com' WHERE name = '易哥'; -------------------------------------------------------------------------------- /示例16/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /示例17/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /示例17/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | 5 | import java.lang.ref.ReferenceQueue; 6 | import java.lang.ref.SoftReference; 7 | import java.lang.ref.WeakReference; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | @SpringBootApplication 12 | public class DemoApplication { 13 | public static void main(String[] args) { 14 | simpleRef(); 15 | refWithReferenceQueue(); 16 | } 17 | 18 | private static void simpleRef() { 19 | // 通过等号直接建立的引用都是强引用 20 | User user = new User(); 21 | 22 | // 通过SoftReference建立的引用是软引用 23 | SoftReference softRefUser =new SoftReference<>(new User()); 24 | 25 | // 通过WeakReference建立的引用是弱引用 26 | WeakReference weakRefUser = new WeakReference<>(new User()); 27 | } 28 | 29 | private static void refWithReferenceQueue() { 30 | // 创建ReferenceQueue 31 | // 即我们的小木桶 32 | ReferenceQueue referenceQueue = new ReferenceQueue<>(); 33 | 34 | // 用来存储弱引用的目标对象 35 | // 即我们用来抓带有雪糕的雪糕棒的手 36 | List weakRefUserList = new ArrayList<>(); 37 | // 创建大量的弱引用对象,交给weakRefUserList引用 38 | // 即创建许多带有雪糕的雪糕棒,并且拿到手里 39 | for (int i =0 ; i< 1000000; i++) { // 创建这么多的目的是为了让内存空间紧张 40 | // 创建弱引用对象,并在此过程中传入ReferenceQueue 41 | // 即将雪糕放到雪糕棒上,并且确定用来收集雪糕棒的小木桶 42 | WeakReference weakReference = new WeakReference(new User(Math.round(Math.random() * 1000)),referenceQueue); 43 | // 引用弱引用对象 44 | // 即抓起这个带有雪糕的雪糕棒 45 | weakRefUserList.add(weakReference); 46 | } 47 | 48 | WeakReference weakReference; 49 | Integer count = 0; 50 | 51 | // 处理被回收的弱引用 52 | // 即通过检查小木桶,处理没有了雪糕的雪糕棒 53 | while ((weakReference = (WeakReference) referenceQueue.poll()) != null) { 54 | // 虽然弱引用存在,但是引用的目标对象已经为空 55 | // 即虽然雪糕棒在木桶中,但是雪糕棒上却没有了雪糕 56 | System.out.println("JVM 清理了:" + weakReference + ", 从WeakReference中取出对象值为:" + weakReference.get()); 57 | count ++; 58 | } 59 | 60 | // 被回收的弱引用总数 61 | // 即小木桶中雪糕棒的数目,也是融化的雪糕的数目 62 | System.out.println("weakReference中的元素数目为:" + count); 63 | 64 | // 在弱引用的目标对象不被清理时,可以引用到目标对象 65 | // 即在雪糕还没有融化掉到地上时,雪糕棒上是有雪糕的 66 | System.out.println("在不被清理的情况下,可以从WeakReference中取出对象值为:" + 67 | new WeakReference(new User(Math.round(Math.random() * 1000)),referenceQueue).get()); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /示例17/src/main/java/com/github/yeecode/mybatisdemo/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class User { 4 | private long id; 5 | 6 | public User() { 7 | } 8 | 9 | public User(long id) { 10 | this.id = id; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return "User:" + id; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /示例17/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例18/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.mybatis.spring.boot 32 | mybatis-spring-boot-starter 33 | 2.0.1 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | 8.0.17 39 | 40 | 41 | org.mybatis 42 | mybatis 43 | 3.5.2 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /示例18/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import org.apache.ibatis.cursor.Cursor; 5 | import org.apache.ibatis.io.Resources; 6 | import org.apache.ibatis.session.SqlSession; 7 | import org.apache.ibatis.session.SqlSessionFactory; 8 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 | import org.springframework.boot.autoconfigure.SpringBootApplication; 10 | 11 | import java.io.IOException; 12 | import java.io.InputStream; 13 | 14 | @SpringBootApplication 15 | public class DemoApplication { 16 | public static void main(String[] args) { 17 | // MyBatis的初始化阶段 18 | String resource = "mybatis-config.xml"; 19 | InputStream inputStream = null; 20 | try { 21 | inputStream = Resources.getResourceAsStream(resource); 22 | } catch (IOException e) { 23 | e.printStackTrace(); 24 | } 25 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 26 | 27 | // 数据读写阶段 28 | try (SqlSession session = sqlSessionFactory.openSession()) { 29 | UserMapper userMapper = session.getMapper(UserMapper.class); 30 | User userParam = new User(); 31 | userParam.setSchoolName("Sunny School"); 32 | Cursor userCursor = userMapper.queryUserBySchoolName(userParam); 33 | for (User user : userCursor) { 34 | System.out.println("name : " + user.getName() + " ; email : " + user.getEmail()); 35 | } 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /示例18/src/main/java/com/github/yeecode/mybatisdemo/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import org.apache.ibatis.cursor.Cursor; 5 | 6 | public interface UserMapper { 7 | Cursor queryUserBySchoolName(User user); 8 | } 9 | -------------------------------------------------------------------------------- /示例18/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class User { 4 | private Integer id; 5 | private String name; 6 | private String email; 7 | private Integer age; 8 | private Integer sex; 9 | private String schoolName; 10 | 11 | 12 | public User() { 13 | } 14 | 15 | public User(String name, String email, Integer age, Integer sex, String schoolName) { 16 | this.name = name; 17 | this.email = email; 18 | this.age = age; 19 | this.sex = sex; 20 | this.schoolName = schoolName; 21 | } 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId(Integer id) { 28 | this.id = id; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public String getEmail() { 40 | return email; 41 | } 42 | 43 | public void setEmail(String email) { 44 | this.email = email; 45 | } 46 | 47 | public Integer getAge() { 48 | return age; 49 | } 50 | 51 | public void setAge(Integer age) { 52 | this.age = age; 53 | } 54 | 55 | public Integer getSex() { 56 | return sex; 57 | } 58 | 59 | public void setSex(Integer sex) { 60 | this.sex = sex; 61 | } 62 | 63 | public String getSchoolName() { 64 | return schoolName; 65 | } 66 | 67 | public void setSchoolName(String schoolName) { 68 | this.schoolName = schoolName; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /示例18/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例18/src/main/resources/com/github/yeecode/mybatisdemo/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 9 | -------------------------------------------------------------------------------- /示例18/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /示例19/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | cglib 29 | cglib 30 | 3.2.9 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-maven-plugin 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /示例19/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.ProxyHandler; 4 | import com.github.yeecode.mybatisdemo.model.User; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cglib.proxy.Enhancer; 7 | 8 | @SpringBootApplication 9 | public class DemoApplication { 10 | public static void main(String[] args) throws Exception { 11 | Enhancer enhancer = new Enhancer(); 12 | // 设置enhancer的回调对象 13 | enhancer.setCallback(new ProxyHandler<>()); 14 | // 设置enhancer对象的父类 15 | enhancer.setSuperclass(User.class); 16 | // 创建代理对象,实际为User的子类 17 | User user = (User) enhancer.create(); 18 | 19 | // 通过代理对象调用目标方法 20 | String ans = user.sayHello("易哥"); 21 | System.out.println(ans); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /示例19/src/main/java/com/github/yeecode/mybatisdemo/model/ProxyHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import org.springframework.cglib.proxy.MethodInterceptor; 4 | import org.springframework.cglib.proxy.MethodProxy; 5 | 6 | import java.lang.reflect.InvocationHandler; 7 | import java.lang.reflect.Method; 8 | 9 | public class ProxyHandler implements MethodInterceptor { 10 | @Override 11 | public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { 12 | System.out.println("before speak"); 13 | Object ans = methodProxy.invokeSuper(o, objects); 14 | System.out.println("after speak"); 15 | return ans; 16 | } 17 | } -------------------------------------------------------------------------------- /示例19/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class User { 4 | public String sayHello(String name) { 5 | System.out.println("hello " + name); 6 | return "OK"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /示例19/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.8.RELEASE 9 | 10 | 11 | com.github.yeecode 12 | mybatisdemo 13 | 0.0.1-SNAPSHOT 14 | mybatisdemo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | org.mybatis.spring.boot 28 | mybatis-spring-boot-starter 29 | 2.1.0 30 | 31 | 32 | 33 | mysql 34 | mysql-connector-java 35 | runtime 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /示例2/src/main/java/com/github/yeecode/mybatisdemo/MainController.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import java.util.List; 8 | 9 | @RestController 10 | @RequestMapping("/") 11 | public class MainController { 12 | @Autowired 13 | private UserMapper userMapper; 14 | 15 | @RequestMapping("/") 16 | public Object index() { 17 | User userParam = new User(); 18 | userParam.setSchoolName("Sunny School"); 19 | List userList = userMapper.queryUserBySchoolName(userParam); 20 | for (User user : userList) { 21 | System.out.println("name : " + user.getName() + " ; email : " + user.getEmail()); 22 | } 23 | return userList; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /示例2/src/main/java/com/github/yeecode/mybatisdemo/MybatisdemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MybatisdemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MybatisdemoApplication.class, args); 11 | System.out.println("Via http://127.0.0.1:8099/ to trigger this demo."); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /示例2/src/main/java/com/github/yeecode/mybatisdemo/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class User { 4 | private Integer id; 5 | private String name; 6 | private String email; 7 | private Integer age; 8 | private Integer sex; 9 | private String schoolName; 10 | 11 | 12 | public Integer getId() { 13 | return id; 14 | } 15 | 16 | public void setId(Integer id) { 17 | this.id = id; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public String getEmail() { 29 | return email; 30 | } 31 | 32 | public void setEmail(String email) { 33 | this.email = email; 34 | } 35 | 36 | public Integer getAge() { 37 | return age; 38 | } 39 | 40 | public void setAge(Integer age) { 41 | this.age = age; 42 | } 43 | 44 | public Integer getSex() { 45 | return sex; 46 | } 47 | 48 | public void setSex(Integer sex) { 49 | this.sex = sex; 50 | } 51 | 52 | public String getSchoolName() { 53 | return schoolName; 54 | } 55 | 56 | public void setSchoolName(String schoolName) { 57 | this.schoolName = schoolName; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /示例2/src/main/java/com/github/yeecode/mybatisdemo/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.apache.ibatis.annotations.Mapper; 4 | 5 | import java.util.List; 6 | 7 | @Mapper 8 | public interface UserMapper { 9 | List queryUserBySchoolName(User user); 10 | } 11 | -------------------------------------------------------------------------------- /示例2/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8099 2 | 3 | spring.datasource.url=jdbc:mysql://127.0.0.1:3306/yeecode?serverTimezone=UTC 4 | spring.datasource.username=root 5 | spring.datasource.password=yeecode 6 | -------------------------------------------------------------------------------- /示例2/src/main/resources/com/github/yeecode/mybatisdemo/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | -------------------------------------------------------------------------------- /示例2/src/test/java/com/github/yeecode/mybatisdemo/MybatisdemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class MybatisdemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /示例20/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.javassist 29 | javassist 30 | 3.25.0-GA 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-maven-plugin 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /示例20/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import javassist.ClassPool; 4 | import javassist.CtClass; 5 | import javassist.CtField; 6 | import javassist.CtMethod; 7 | import javassist.CtNewMethod; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | import java.lang.reflect.Method; 11 | 12 | @SpringBootApplication 13 | public class DemoApplication { 14 | public static void main(String[] args) throws Exception { 15 | ClassPool pool = ClassPool.getDefault(); 16 | // 定义一个类 17 | CtClass userCtClazz = pool.makeClass("com.github.yeecode.mybatisdemo.User"); 18 | // 创建name属性 19 | CtField nameField = new CtField(pool.get("java.lang.String"), "name", userCtClazz); 20 | userCtClazz.addField(nameField); 21 | // 创建name的setter 22 | CtMethod setMethod = CtNewMethod.make("public void setName(String name) { this.name = name;}", userCtClazz); 23 | userCtClazz.addMethod(setMethod); 24 | // 创建sayHello方法 25 | CtMethod sayHello = CtNewMethod.make("public String sayHello() { return \"Hello, I am \" + this.name ;}", userCtClazz); 26 | userCtClazz.addMethod(sayHello); 27 | 28 | Class userClazz = userCtClazz.toClass(); 29 | // 创建一个对象 30 | Object user = userClazz.newInstance(); 31 | // 为对象设置name值 32 | Method[] methods = userClazz.getMethods(); 33 | for (Method method: methods){ 34 | if (method.getName().equals("setName")) { 35 | method.invoke(user,"易哥"); 36 | } 37 | } 38 | // 调用对象sayHello方法 39 | for (Method method: methods){ 40 | if (method.getName().equals("sayHello")) { 41 | String result = (String) method.invoke(user); 42 | System.out.println(result); 43 | 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /示例20/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例21/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-maven-plugin 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /示例21/src/main/java/com/github/yeecode/mybatisdemo/model/UserModel01.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class UserModel01 implements Serializable { 6 | private static final long serialVerisionUID = 123L ; 7 | 8 | private Integer id; 9 | private String name; 10 | private String description; 11 | 12 | public Integer getId() { 13 | return id; 14 | } 15 | 16 | public void setId(Integer id) { 17 | this.id = id; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public String getDescription() { 29 | return description; 30 | } 31 | 32 | public void setDescription(String description) { 33 | this.description = description; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /示例21/src/main/java/com/github/yeecode/mybatisdemo/model/UserModel02.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.Externalizable; 4 | import java.io.IOException; 5 | import java.io.ObjectInput; 6 | import java.io.ObjectOutput; 7 | 8 | public class UserModel02 implements Externalizable { 9 | private static final long serialVerisionUID = 1L; 10 | 11 | private Integer id; 12 | private String name; 13 | private String description; 14 | 15 | public Integer getId() { 16 | return id; 17 | } 18 | 19 | public void setId(Integer id) { 20 | this.id = id; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public String getDescription() { 32 | return description; 33 | } 34 | 35 | public void setDescription(String description) { 36 | this.description = description; 37 | } 38 | 39 | @Override 40 | public void writeExternal(ObjectOutput out) throws IOException { 41 | System.out.println("writeExternal doing ..."); 42 | out.write(id); // DataOutput中的方法 43 | out.writeObject(name + "(from writeExternal)"); 44 | } 45 | 46 | @Override 47 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 48 | System.out.println("readExternal doing ..."); 49 | id = in.read(); 50 | name = (String) in.readObject(); 51 | System.out.println("name in file is:" + name); 52 | name = name + "(from readExternal)"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /示例21/src/main/java/com/github/yeecode/mybatisdemo/model/UserModel03.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.ObjectStreamException; 4 | import java.io.Serializable; 5 | 6 | public class UserModel03 implements Serializable { 7 | private static final long serialVerisionUID = 123L; 8 | 9 | private Integer id; 10 | private String name; 11 | private String description; 12 | 13 | public Integer getId() { 14 | return id; 15 | } 16 | 17 | public void setId(Integer id) { 18 | this.id = id; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getDescription() { 30 | return description; 31 | } 32 | 33 | public void setDescription(String description) { 34 | this.description = description; 35 | } 36 | 37 | private Object writeReplace() throws ObjectStreamException { 38 | System.out.println("writeReplace doing ..."); 39 | UserModel03 userModel = new UserModel03(); 40 | userModel.setId(2); 41 | userModel.setName("yeecode"); 42 | userModel.setDescription("description from writeReplace"); 43 | return userModel; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /示例21/src/main/java/com/github/yeecode/mybatisdemo/model/UserModel04.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.ObjectStreamException; 4 | import java.io.Serializable; 5 | 6 | public class UserModel04 implements Serializable { 7 | private static final long serialVerisionUID = 123L ; 8 | 9 | private Integer id; 10 | private String name; 11 | private String description; 12 | 13 | public Integer getId() { 14 | return id; 15 | } 16 | 17 | public void setId(Integer id) { 18 | this.id = id; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getDescription() { 30 | return description; 31 | } 32 | 33 | public void setDescription(String description) { 34 | this.description = description; 35 | } 36 | 37 | private Object readResolve() throws ObjectStreamException { 38 | System.out.println("readResolve doing ..."); 39 | UserModel04 userModel = new UserModel04(); 40 | userModel.setId(2); 41 | userModel.setName("yeecode"); 42 | userModel.setDescription("description from readResolve"); 43 | return userModel; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /示例21/src/main/java/com/github/yeecode/mybatisdemo/model/UserModel05.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.Externalizable; 4 | import java.io.IOException; 5 | import java.io.ObjectInput; 6 | import java.io.ObjectOutput; 7 | import java.io.ObjectStreamException; 8 | 9 | public class UserModel05 implements Externalizable { 10 | private static final long serialVerisionUID = 1L; 11 | 12 | private Integer id; 13 | private String name; 14 | private String description; 15 | 16 | public Integer getId() { 17 | return id; 18 | } 19 | 20 | public void setId(Integer id) { 21 | this.id = id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void setName(String name) { 29 | this.name = name; 30 | } 31 | 32 | public String getDescription() { 33 | return description; 34 | } 35 | 36 | public void setDescription(String description) { 37 | this.description = description; 38 | } 39 | 40 | @Override 41 | public void writeExternal(ObjectOutput out) throws IOException { 42 | System.out.println("writeExternal doing ..."); 43 | out.write(id); 44 | out.writeObject(name + "(from writeExternal)"); 45 | } 46 | 47 | @Override 48 | public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 49 | System.out.println("readExternal doing ..."); 50 | id = in.read(); 51 | name = (String) in.readObject(); 52 | System.out.println("name in file is:" + name); 53 | name = name + "(from readExternal)"; 54 | } 55 | 56 | private Object writeReplace() throws ObjectStreamException { 57 | System.out.println("writeReplace doing ..."); 58 | UserModel05 userModel = new UserModel05(); 59 | userModel.setId(2); 60 | userModel.setName(name + "(from writeReplace)"); 61 | userModel.setDescription("description from writeReplace"); 62 | return userModel; 63 | } 64 | 65 | private Object readResolve() throws ObjectStreamException { 66 | System.out.println("readResolve doing ..."); 67 | UserModel05 userModel = new UserModel05(); 68 | userModel.setId(2); 69 | userModel.setName(name + "(from readResolve)"); 70 | userModel.setDescription("description from readResolve"); 71 | return userModel; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /示例21/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例22/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-maven-plugin 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /示例22/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | 5 | @SpringBootApplication 6 | public class DemoApplication { 7 | // 创建了两个ThreadLocal变量 8 | private static ThreadLocal threadLocalNumber = new ThreadLocal<>(); 9 | private static ThreadLocal threadLocalString = new ThreadLocal<>(); 10 | 11 | public static void main(String[] args) { 12 | try { 13 | Thread thread01 = new Thread(new Task01()); 14 | Thread thread02 = new Thread(new Task02()); 15 | thread01.start(); 16 | thread02.start(); 17 | Thread.sleep(2L); 18 | System.out.println("Main-number: " + threadLocalNumber.get()); 19 | System.out.println("Main-string: " + threadLocalString.get()); 20 | } catch (Exception ex) { 21 | ex.printStackTrace(); 22 | } 23 | 24 | } 25 | 26 | private static class Task01 implements Runnable { 27 | @Override 28 | public void run() { 29 | System.out.println("Thread01-number: " + threadLocalNumber.get()); 30 | System.out.println("Set Thread01-number : 3001"); 31 | threadLocalNumber.set(3001); 32 | System.out.println("Thread01-number: " + threadLocalNumber.get()); 33 | System.out.println("Set Thread01-string : hello thread01"); 34 | threadLocalString.set("hello thread01"); 35 | System.out.println("Thread01-string: " + threadLocalString.get()); 36 | } 37 | } 38 | 39 | private static class Task02 implements Runnable { 40 | @Override 41 | public void run() { 42 | System.out.println("Set Thread02-number : 3002"); 43 | threadLocalNumber.set(3002); 44 | System.out.println("Thread02-number: " + threadLocalNumber.get()); 45 | System.out.println("Thread02-string: " + threadLocalString.get()); 46 | System.out.println("Set Thread02-string : hello thread02"); 47 | threadLocalString.set("hello thread02"); 48 | System.out.println("Thread02-string: " + threadLocalString.get()); 49 | } 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /示例22/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例23/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | mysql 29 | mysql-connector-java 30 | 8.0.17 31 | 32 | 33 | 34 | org.mybatis 35 | mybatis 36 | 3.5.2 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-maven-plugin 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /示例23/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import org.apache.ibatis.io.Resources; 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | 13 | @SpringBootApplication 14 | public class DemoApplication { 15 | public static void main(String[] args) { 16 | String resource = "mybatis-config.xml"; 17 | InputStream inputStream = null; 18 | try { 19 | inputStream = Resources.getResourceAsStream(resource); 20 | } catch (IOException e) { 21 | e.printStackTrace(); 22 | } 23 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 24 | 25 | try (SqlSession session = sqlSessionFactory.openSession()) { 26 | User user01 = new User("张大壮","dazhuang@sample.mail",18, 0, "XIERQI SCHOOL"); 27 | User user02 = new User("王小壮","xiaozhuang@sample.mail",17, 0, "GAOKE SCHOOL"); 28 | User user03 = new User("王二壮", "wangerzhuang@sample.mail",15,0, "GAOKE SCHOOL"); 29 | User user04 = new User("李二壮", "lierzhuang@sample.com",21,0, "KEYUAN SCHOOL"); 30 | 31 | int result; 32 | 33 | // 注意,该实验结论基于MyBatis数据库,并且已经对id字段开启了主键自增设置。 34 | // 如果采用其他数据库或者采用其他设置,将得到不同的结果。 35 | 36 | // 使用Jdbc3KeyGenerator 37 | System.out.println("user01:"); 38 | System.out.println("before insert :" + user01.toString()); 39 | result = session.insert("com.github.yeecode.mybatisdemo.dao.UserDao.addUser_A", user01); 40 | System.out.println("insert result : " + result); 41 | System.out.println("after insert :" + user01.toString()); 42 | 43 | // 使用SelectKeyGenerator(AFTER) 44 | System.out.println("user02:"); 45 | System.out.println("before insert :" + user02.toString()); 46 | result = session.insert("com.github.yeecode.mybatisdemo.dao.UserDao.addUser_B", user02); 47 | System.out.println("insert result : " + result); 48 | System.out.println("after insert :" + user02.toString()); 49 | 50 | // 使用SelectKeyGenerator(BEFORE) 51 | // 此种设置用于不支持id自增或未启用id自增的数据库 52 | // 由于我们的数据库设置了id自增,会忽略传入的id值。因此会引发user03对象的变化,但是并不会生效的数据库中。 53 | System.out.println("user03:"); 54 | System.out.println("before insert :" + user03.toString()); 55 | result = session.insert("com.github.yeecode.mybatisdemo.dao.UserDao.addUser_C", user03); 56 | System.out.println("insert result : " + result); 57 | System.out.println("after insert :" + user03.toString()); 58 | 59 | // 使用NoKeyGenerator,即不使用主键自增功能 60 | System.out.println("user04:"); 61 | System.out.println("before insert :" + user04.toString()); 62 | result = session.insert("com.github.yeecode.mybatisdemo.dao.UserDao.addUser_D", user04); 63 | System.out.println("insert result : " + result); 64 | System.out.println("after insert :" + user04.toString()); 65 | 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /示例23/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class User implements Serializable { 6 | private Integer id; 7 | private String name; 8 | private String email; 9 | private Integer age; 10 | private Integer sex; 11 | private String schoolName; 12 | 13 | public User() { 14 | } 15 | 16 | public User(String name, String email, Integer age, Integer sex, String schoolName) { 17 | this.name = name; 18 | this.email = email; 19 | this.age = age; 20 | this.sex = sex; 21 | this.schoolName = schoolName; 22 | } 23 | 24 | public Integer getId() { 25 | return id; 26 | } 27 | 28 | public void setId(Integer id) { 29 | this.id = id; 30 | } 31 | 32 | public String getName() { 33 | return name; 34 | } 35 | 36 | public void setName(String name) { 37 | this.name = name; 38 | } 39 | 40 | public String getEmail() { 41 | return email; 42 | } 43 | 44 | public void setEmail(String email) { 45 | this.email = email; 46 | } 47 | 48 | public Integer getAge() { 49 | return age; 50 | } 51 | 52 | public void setAge(Integer age) { 53 | this.age = age; 54 | } 55 | 56 | public Integer getSex() { 57 | return sex; 58 | } 59 | 60 | public void setSex(Integer sex) { 61 | this.sex = sex; 62 | } 63 | 64 | public String getSchoolName() { 65 | return schoolName; 66 | } 67 | 68 | public void setSchoolName(String schoolName) { 69 | this.schoolName = schoolName; 70 | } 71 | 72 | @Override 73 | public String toString() { 74 | return "User{" + 75 | "id=" + id + 76 | ", name='" + name + '\'' + 77 | ", email='" + email + '\'' + 78 | ", age='" + age + '\'' + 79 | ", sex=" + sex + 80 | ", schoolName='" + schoolName + '\'' + 81 | '}'; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /示例23/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例23/src/main/resources/com/github/yeecode/mybatisdemo/UserDao.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | INSERT INTO `user` 8 | (`name`,`email`,`age`,`sex`,`schoolName`) 9 | VALUES 10 | (#{name},#{email},#{age},#{sex},#{schoolName}) 11 | 12 | 13 | 14 | 15 | SELECT LAST_INSERT_ID() 16 | 17 | INSERT INTO `user` 18 | (`name`,`email`,`age`,`sex`,`schoolName`) 19 | VALUES 20 | (#{name},#{email},#{age},#{sex},#{schoolName}) 21 | 22 | 23 | 24 | 25 | SELECT CEILING(RAND()*900+100) 26 | -- SELECT LAST_INSERT_ID() 27 | 28 | INSERT INTO `user` 29 | (`name`,`email`,`age`,`sex`,`schoolName`) 30 | VALUES 31 | (#{name},#{email},#{age},#{sex},#{schoolName}) 32 | 33 | 34 | 35 | INSERT INTO `user` 36 | (`name`,`email`,`age`,`sex`,`schoolName`) 37 | VALUES 38 | (#{name},#{email},#{age},#{sex},#{schoolName}) 39 | 40 | -------------------------------------------------------------------------------- /示例23/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /示例24/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | mysql 29 | mysql-connector-java 30 | 8.0.17 31 | 32 | 33 | 34 | org.mybatis 35 | mybatis 36 | 3.5.2 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-maven-plugin 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /示例24/src/main/java/com/github/yeecode/mybatisdemo/model/Task.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class Task { 4 | private Integer id; 5 | private Integer userId; 6 | private String taskName; 7 | 8 | public Integer getId() { 9 | return id; 10 | } 11 | 12 | public void setId(Integer id) { 13 | this.id = id; 14 | } 15 | 16 | public Integer getUserId() { 17 | return userId; 18 | } 19 | 20 | public void setUserId(Integer userId) { 21 | this.userId = userId; 22 | } 23 | 24 | public String getTaskName() { 25 | return taskName; 26 | } 27 | 28 | public void setTaskName(String taskName) { 29 | this.taskName = taskName; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /示例24/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | public class User implements Serializable { 7 | private Integer id; 8 | private String name; 9 | private String email; 10 | private Integer age; 11 | private Integer sex; 12 | private String schoolName; 13 | private List taskList; 14 | 15 | public User() { 16 | } 17 | 18 | 19 | public Integer getId() { 20 | return id; 21 | } 22 | 23 | public void setId(Integer id) { 24 | this.id = id; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public String getEmail() { 36 | return email; 37 | } 38 | 39 | public void setEmail(String email) { 40 | this.email = email; 41 | } 42 | 43 | public Integer getAge() { 44 | return age; 45 | } 46 | 47 | public void setAge(Integer age) { 48 | this.age = age; 49 | } 50 | 51 | public Integer getSex() { 52 | return sex; 53 | } 54 | 55 | public void setSex(Integer sex) { 56 | this.sex = sex; 57 | } 58 | 59 | public String getSchoolName() { 60 | return schoolName; 61 | } 62 | 63 | public void setSchoolName(String schoolName) { 64 | this.schoolName = schoolName; 65 | } 66 | 67 | public List getTaskList() { 68 | return taskList; 69 | } 70 | 71 | public void setTaskList(List taskList) { 72 | this.taskList = taskList; 73 | } 74 | 75 | public String toString() { 76 | StringBuilder stringBuilder = new StringBuilder(); 77 | stringBuilder.append("User{id=").append(id).append(", name=").append(name).append('}').append("\n") ; 78 | stringBuilder.append("taskList: \n"); 79 | for (Task task : taskList) { 80 | stringBuilder.append(task.getId()).append(" : ").append(task.getTaskName()).append("\n"); 81 | } 82 | return stringBuilder.toString(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /示例24/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例24/src/main/resources/com/github/yeecode/mybatisdemo/UserDao.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 36 | 37 | 40 | 41 | 44 | 45 | -------------------------------------------------------------------------------- /示例24/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /示例25/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.mybatis.spring.boot 40 | mybatis-spring-boot-starter 41 | 2.0.1 42 | 43 | 44 | 45 | 46 | mysql 47 | mysql-connector-java 48 | 8.0.17 49 | 50 | 51 | 52 | 53 | org.mybatis 54 | mybatis 55 | 3.5.2 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-maven-plugin 66 | 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /示例25/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import org.apache.ibatis.cursor.Cursor; 5 | import org.apache.ibatis.io.Resources; 6 | import org.apache.ibatis.session.SqlSession; 7 | import org.apache.ibatis.session.SqlSessionFactory; 8 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 | import org.springframework.boot.autoconfigure.SpringBootApplication; 10 | 11 | import java.io.IOException; 12 | import java.io.InputStream; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | @SpringBootApplication 18 | public class DemoApplication { 19 | public static void main(String[] args) { 20 | // MyBatis的初始化阶段 21 | String resource = "mybatis-config.xml"; 22 | InputStream inputStream = null; 23 | try { 24 | inputStream = Resources.getResourceAsStream(resource); 25 | } catch (IOException e) { 26 | e.printStackTrace(); 27 | } 28 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 29 | 30 | // 数据读写阶段 31 | try (SqlSession session = sqlSessionFactory.openSession()) { 32 | // User userParam = new User(); 33 | // userParam.setSchoolName("Sunny School"); 34 | // List userList = session.selectList("com.github.yeecode.mybatisdemo.dao.UserMapper.queryUserBySchoolName_A", userParam); 35 | // for (User user : userList) { 36 | // System.out.println("name : " + user.getName() + " ; email : " + user.getEmail()); 37 | // } 38 | // 39 | // userList = session.selectList("com.github.yeecode.mybatisdemo.dao.UserMapper.queryUserBySchoolName_B", userParam); 40 | // for (User user : userList) { 41 | // System.out.println("name : " + user.getName() + " ; email : " + user.getEmail()); 42 | // } 43 | 44 | Map param = new HashMap<>(); 45 | param.put("ageMinLimit",10); 46 | param.put("ageMaxLimit",30); 47 | session.selectOne("com.github.yeecode.mybatisdemo.dao.UserMapper.runCall",param); 48 | System.out.println("proceduce param :" + param); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /示例25/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class User { 4 | private Integer id; 5 | private String name; 6 | private String email; 7 | private Integer age; 8 | private Integer sex; 9 | private String schoolName; 10 | 11 | 12 | public User() { 13 | } 14 | 15 | public User(String name, String email, Integer age, Integer sex, String schoolName) { 16 | this.name = name; 17 | this.email = email; 18 | this.age = age; 19 | this.sex = sex; 20 | this.schoolName = schoolName; 21 | } 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId(Integer id) { 28 | this.id = id; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public String getEmail() { 40 | return email; 41 | } 42 | 43 | public void setEmail(String email) { 44 | this.email = email; 45 | } 46 | 47 | public Integer getAge() { 48 | return age; 49 | } 50 | 51 | public void setAge(Integer age) { 52 | this.age = age; 53 | } 54 | 55 | public Integer getSex() { 56 | return sex; 57 | } 58 | 59 | public void setSex(Integer sex) { 60 | this.sex = sex; 61 | } 62 | 63 | public String getSchoolName() { 64 | return schoolName; 65 | } 66 | 67 | public void setSchoolName(String schoolName) { 68 | this.schoolName = schoolName; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /示例25/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例25/src/main/resources/com/github/yeecode/mybatisdemo/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 29 | -------------------------------------------------------------------------------- /示例25/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /示例25/src/test/java/com/github/yeecode/mybatisdemo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /示例26/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | mysql 35 | mysql-connector-java 36 | 8.0.16 37 | runtime 38 | 39 | 40 | 41 | 42 | mysql 43 | mysql-connector-java 44 | 8.0.17 45 | 46 | 47 | 48 | 49 | org.mybatis 50 | mybatis 51 | 3.5.2 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-maven-plugin 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /示例26/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import org.apache.ibatis.io.Resources; 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | @SpringBootApplication 16 | public class DemoApplication { 17 | public static void main(String[] args) { 18 | String resource = "mybatis-config.xml"; 19 | InputStream inputStream = null; 20 | try { 21 | inputStream = Resources.getResourceAsStream(resource); 22 | } catch (IOException e) { 23 | e.printStackTrace(); 24 | } 25 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 26 | 27 | // 输出包含两个结果的结果集 28 | System.out.println("--输出包含多个结果的结果集--"); 29 | try (SqlSession session = sqlSessionFactory.openSession()) { 30 | List> result = session.selectList("com.github.yeecode.mybatisdemo.dao.Dao01.query"); 31 | System.out.println("结果集包含结果数目:" + result.size()); 32 | int i = 1; 33 | for (List list: result ) { 34 | System.out.println("结果集" + i++ + ":"); 35 | for (Object o: list) { 36 | System.out.println(o.toString()); 37 | } 38 | } 39 | } 40 | 41 | // 两个结果合并为一个结果 42 | System.out.println("--多个结果整合为一个结果--"); 43 | try (SqlSession session = sqlSessionFactory.openSession()) { 44 | List result = session.selectList("com.github.yeecode.mybatisdemo.dao.Dao02.query"); 45 | System.out.println("结果包含记录数目: " + result.size()); 46 | for (User user: result) { 47 | System.out.println(user); 48 | } 49 | } 50 | } 51 | } 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /示例26/src/main/java/com/github/yeecode/mybatisdemo/model/Task.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class Task { 4 | private Integer id; 5 | private Integer userId; 6 | private String taskName; 7 | 8 | public Integer getId() { 9 | return id; 10 | } 11 | 12 | public void setId(Integer id) { 13 | this.id = id; 14 | } 15 | 16 | public Integer getUserId() { 17 | return userId; 18 | } 19 | 20 | public void setUserId(Integer userId) { 21 | this.userId = userId; 22 | } 23 | 24 | public String getTaskName() { 25 | return taskName; 26 | } 27 | 28 | public void setTaskName(String taskName) { 29 | this.taskName = taskName; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "task : " + id + " : " + taskName; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /示例26/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | public class User implements Serializable { 7 | private Integer id; 8 | private String name; 9 | private String email; 10 | private Integer age; 11 | private Integer sex; 12 | private String schoolName; 13 | private List taskList; 14 | 15 | public Integer getId() { 16 | return id; 17 | } 18 | 19 | public void setId(Integer id) { 20 | this.id = id; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | 31 | public String getEmail() { 32 | return email; 33 | } 34 | 35 | public void setEmail(String email) { 36 | this.email = email; 37 | } 38 | 39 | public Integer getAge() { 40 | return age; 41 | } 42 | 43 | public void setAge(Integer age) { 44 | this.age = age; 45 | } 46 | 47 | public Integer getSex() { 48 | return sex; 49 | } 50 | 51 | public void setSex(Integer sex) { 52 | this.sex = sex; 53 | } 54 | 55 | public String getSchoolName() { 56 | return schoolName; 57 | } 58 | 59 | public void setSchoolName(String schoolName) { 60 | this.schoolName = schoolName; 61 | } 62 | 63 | public List getTaskList() { 64 | return taskList; 65 | } 66 | 67 | public void setTaskList(List taskList) { 68 | this.taskList = taskList; 69 | } 70 | 71 | @Override 72 | public String toString() { 73 | String ans = "user : " + id + " : " + name + "\n"; 74 | if (taskList != null) { 75 | ans += "Tasks: \n"; 76 | for (Task task: taskList) { 77 | ans += task.getTaskName() + "\n"; 78 | } 79 | } 80 | return ans; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /示例26/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例26/src/main/resources/com/github/yeecode/mybatisdemo/Dao01.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 32 | 33 | -------------------------------------------------------------------------------- /示例26/src/main/resources/com/github/yeecode/mybatisdemo/Dao02.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 32 | 33 | -------------------------------------------------------------------------------- /示例26/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /示例26/src/test/java/com/github/yeecode/mybatisdemo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /示例27/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /示例27/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.Performer; 4 | import com.github.yeecode.mybatisdemo.processor.ContactOfficer; 5 | import com.github.yeecode.mybatisdemo.processor.Handler; 6 | import com.github.yeecode.mybatisdemo.processor.MailSender; 7 | import com.github.yeecode.mybatisdemo.processor.MaterialManager; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | import java.util.Arrays; 11 | import java.util.List; 12 | 13 | @SpringBootApplication 14 | public class DemoApplication { 15 | public static void main(String[] args) { 16 | Performer performer01 = new Performer("易哥", "yeecode@sample.com", 18, 0, "Sunny School"); 17 | Performer performer02 = new Performer("莉莉", "lili@sample.com", 15, 1, "Garden School"); 18 | Performer performer03 = new Performer("杰克", "jack@sample.com", 25, 0, "Sunny School"); 19 | Performer performer04 = new Performer("张大壮", "zdazhaung@sample.com", 16, 0, "Garden School"); 20 | 21 | List performerList = Arrays.asList(new Performer[]{performer01, performer02, performer03, performer04}); 22 | 23 | // 不使用责任链模式 24 | System.out.println("不使用责任链模式:"); 25 | // 创建三个工作人员实例 26 | MailSender mailSender = new MailSender(); 27 | MaterialManager materialManager = new MaterialManager(); 28 | ContactOfficer contactOfficer = new ContactOfficer(); 29 | // 依次处理每个参与者 30 | for (Performer performer : performerList) { 31 | System.out.println("process " + performer.getName() + ":"); 32 | new MailSender().handle(performer); 33 | new MaterialManager().handle(performer); 34 | new ContactOfficer().handle(performer); 35 | System.out.println("---------"); 36 | } 37 | 38 | // 使用责任链模式 39 | System.out.println("使用责任链模式:"); 40 | // 创建责任链 41 | Handler handlerChain = new MailSender(); 42 | handlerChain.setNextHandler(new MaterialManager()).setNextHandler(new ContactOfficer()); 43 | 44 | // 依次处理每个参与者 45 | for (Performer performer : performerList) { 46 | System.out.println("process " + performer.getName() + ":"); 47 | handlerChain.triggerProcess(performer); 48 | System.out.println("---------"); 49 | } 50 | 51 | } 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /示例27/src/main/java/com/github/yeecode/mybatisdemo/model/Performer.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class Performer { 4 | private Integer id; 5 | private String name; 6 | private String email; 7 | private Integer age; 8 | private Integer sex; 9 | private String schoolName; 10 | 11 | 12 | public Performer(String name, String email, Integer age, Integer sex, String schoolName) { 13 | this.name = name; 14 | this.email = email; 15 | this.age = age; 16 | this.sex = sex; 17 | this.schoolName = schoolName; 18 | } 19 | 20 | public Integer getId() { 21 | return id; 22 | } 23 | 24 | public void setId(Integer id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public String getEmail() { 37 | return email; 38 | } 39 | 40 | public void setEmail(String email) { 41 | this.email = email; 42 | } 43 | 44 | public Integer getAge() { 45 | return age; 46 | } 47 | 48 | public void setAge(Integer age) { 49 | this.age = age; 50 | } 51 | 52 | public Integer getSex() { 53 | return sex; 54 | } 55 | 56 | public void setSex(Integer sex) { 57 | this.sex = sex; 58 | } 59 | 60 | public String getSchoolName() { 61 | return schoolName; 62 | } 63 | 64 | public void setSchoolName(String schoolName) { 65 | this.schoolName = schoolName; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /示例27/src/main/java/com/github/yeecode/mybatisdemo/processor/ContactOfficer.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.processor; 2 | 3 | import com.github.yeecode.mybatisdemo.model.Performer; 4 | 5 | public class ContactOfficer extends Handler { 6 | @Override 7 | public void handle (Performer performer) { 8 | if(performer.getAge() < 18) { 9 | System.out.println("Arrange school bus for " + performer.getName()); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /示例27/src/main/java/com/github/yeecode/mybatisdemo/processor/Handler.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.processor; 2 | 3 | import com.github.yeecode.mybatisdemo.model.Performer; 4 | 5 | public abstract class Handler { 6 | // 当前处理器的下一个处理器 7 | private Handler nextHandler; 8 | 9 | /** 10 | * 当前处理器的处理逻辑,交给子类实现 11 | * @param performer 被处理对象 12 | */ 13 | public abstract void handle(Performer performer); 14 | 15 | /** 16 | * 触发当前处理器,并在处理结束后将被处理对象传给后续处理器 17 | * @param performer 被处理对象 18 | */ 19 | public void triggerProcess(Performer performer) { 20 | handle(performer); 21 | if (nextHandler != null) { 22 | nextHandler.triggerProcess(performer); 23 | } 24 | } 25 | 26 | /** 27 | * 设置当前处理器的下一个处理器 28 | * @param nextHandler 下一个处理器 29 | * @return 下一个处理器 30 | */ 31 | public Handler setNextHandler(Handler nextHandler) { 32 | this.nextHandler = nextHandler; 33 | return nextHandler; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /示例27/src/main/java/com/github/yeecode/mybatisdemo/processor/MailSender.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.processor; 2 | 3 | import com.github.yeecode.mybatisdemo.model.Performer; 4 | 5 | public class MailSender extends Handler { 6 | @Override 7 | public void handle(Performer performer) { 8 | if (performer.getEmail() != null) { 9 | System.out.println("Send Email to " + performer.getEmail()); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /示例27/src/main/java/com/github/yeecode/mybatisdemo/processor/MaterialManager.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.processor; 2 | 3 | import com.github.yeecode.mybatisdemo.model.Performer; 4 | 5 | public class MaterialManager extends Handler { 6 | @Override 7 | public void handle(Performer performer) { 8 | System.out.println("Prepare clothes for a " + (performer.getSex() == 0 ? "Boy" : "Girl")); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /示例27/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例28/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | 29 | mysql 30 | mysql-connector-java 31 | 8.0.17 32 | 33 | 34 | 35 | 36 | org.mybatis 37 | mybatis 38 | 3.5.2 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /示例28/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.User; 4 | import org.apache.ibatis.io.Resources; 5 | import org.apache.ibatis.session.SqlSession; 6 | import org.apache.ibatis.session.SqlSessionFactory; 7 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.util.List; 13 | 14 | @SpringBootApplication 15 | public class DemoApplication { 16 | public static void main(String[] args) { 17 | // MyBatis的初始化阶段 18 | String resource = "mybatis-config.xml"; 19 | InputStream inputStream = null; 20 | try { 21 | inputStream = Resources.getResourceAsStream(resource); 22 | } catch (IOException e) { 23 | e.printStackTrace(); 24 | } 25 | SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 26 | 27 | // 数据读写阶段 28 | try (SqlSession session = sqlSessionFactory.openSession()) { 29 | User userParam = new User(); 30 | userParam.setSchoolName("Sunny School"); 31 | List userList = session.selectList("com.github.yeecode.mybatisdemo.dao.UserMapper.queryUserBySchoolName", userParam); 32 | for (User user : userList) { 33 | System.out.println("name : " + user.getName() + " ; email : " + user.getEmail()); 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /示例28/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class User { 4 | private Integer id; 5 | private String name; 6 | private String email; 7 | private Integer age; 8 | private Integer sex; 9 | private String schoolName; 10 | 11 | 12 | public User() { 13 | } 14 | 15 | public User(String name, String email, Integer age, Integer sex, String schoolName) { 16 | this.name = name; 17 | this.email = email; 18 | this.age = age; 19 | this.sex = sex; 20 | this.schoolName = schoolName; 21 | } 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId(Integer id) { 28 | this.id = id; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public String getEmail() { 40 | return email; 41 | } 42 | 43 | public void setEmail(String email) { 44 | this.email = email; 45 | } 46 | 47 | public Integer getAge() { 48 | return age; 49 | } 50 | 51 | public void setAge(Integer age) { 52 | this.age = age; 53 | } 54 | 55 | public Integer getSex() { 56 | return sex; 57 | } 58 | 59 | public void setSex(Integer sex) { 60 | this.sex = sex; 61 | } 62 | 63 | public String getSchoolName() { 64 | return schoolName; 65 | } 66 | 67 | public void setSchoolName(String schoolName) { 68 | this.schoolName = schoolName; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /示例28/src/main/java/com/github/yeecode/mybatisdemo/plugin/YeecodeInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.plugin; 2 | 3 | import org.apache.ibatis.executor.resultset.ResultSetHandler; 4 | import org.apache.ibatis.plugin.Interceptor; 5 | import org.apache.ibatis.plugin.Intercepts; 6 | import org.apache.ibatis.plugin.Invocation; 7 | import org.apache.ibatis.plugin.Plugin; 8 | import org.apache.ibatis.plugin.Signature; 9 | 10 | import java.sql.Statement; 11 | import java.util.List; 12 | import java.util.Properties; 13 | 14 | 15 | @Intercepts({ 16 | @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}) 17 | }) 18 | public class YeecodeInterceptor implements Interceptor { 19 | private String info; 20 | 21 | @Override 22 | public Object intercept(Invocation invocation) throws Throwable { 23 | // 执行原有方法 24 | Object result = invocation.proceed(); 25 | // 打印原方法输出结果的数目 26 | System.out.println(info + ":" + ((List) result).size()); 27 | // 返回原有结果 28 | return result; 29 | } 30 | 31 | @Override 32 | public void setProperties(Properties properties) { 33 | // 为拦截器设置属性 34 | info = properties.get("preInfo").toString(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /示例28/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例28/src/main/resources/com/github/yeecode/mybatisdemo/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 9 | 10 | 11 | INSERT INTO `user` 12 | (`name`,`email`,`age`,`sex`,`schoolName`) 13 | VALUES 14 | (#{name},#{email},#{age},#{sex},#{schoolName}) 15 | 16 | -------------------------------------------------------------------------------- /示例28/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /示例29/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | 29 | com.google.guava 30 | guava 31 | 28.1-jre 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /示例29/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.google.common.collect.ArrayListMultimap; 4 | import com.google.common.collect.Multimap; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | import java.util.ArrayList; 8 | import java.util.HashMap; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | @SpringBootApplication 13 | public class DemoApplication { 14 | public static void main(String[] args) { 15 | String[] record = {"易哥:4","杰克:5","易哥:1","杰克:7","张大壮:3"}; 16 | 17 | System.out.println("使用原生Map:"); 18 | Map> taskMap = new HashMap<>(); 19 | for (String item : record) { 20 | String key = item.split(":")[0]; 21 | Integer value = Integer.parseInt(item.split(":")[1]); 22 | taskMap.putIfAbsent(key,new ArrayList<>()); 23 | taskMap.get(key).add(value); 24 | } 25 | System.out.println(taskMap); 26 | 27 | System.out.println("使用Multimap:"); 28 | Multimap multimap = ArrayListMultimap.create(); 29 | for (String item : record) { 30 | String key = item.split(":")[0]; 31 | Integer value = Integer.parseInt(item.split(":")[1]); 32 | multimap.put(key,value); 33 | } 34 | System.out.println(multimap.asMap()); 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /示例29/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例3/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | mysql 28 | mysql-connector-java 29 | 8.0.17 30 | 31 | 32 | org.mybatis 33 | mybatis 34 | 3.5.2 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-maven-plugin 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /示例3/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.apache.ibatis.io.Resources; 4 | import org.apache.ibatis.session.SqlSession; 5 | import org.apache.ibatis.session.SqlSessionFactory; 6 | import org.apache.ibatis.session.SqlSessionFactoryBuilder; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import java.util.List; 12 | 13 | @SpringBootApplication 14 | public class DemoApplication { 15 | public static void main(String[] args) { 16 | // 第一阶段:MyBatis的初始化阶段 17 | String resource = "mybatis-config.xml"; 18 | // 得到配置文件的输入流 19 | InputStream inputStream = null; 20 | try { 21 | inputStream = Resources.getResourceAsStream(resource); 22 | } catch (IOException e) { 23 | e.printStackTrace(); 24 | } 25 | // 得到SqlSessionFactory 26 | SqlSessionFactory sqlSessionFactory = 27 | new SqlSessionFactoryBuilder().build(inputStream); 28 | 29 | // 第二阶段:数据读写阶段 30 | try (SqlSession session = sqlSessionFactory.openSession()) { 31 | // 找到接口对应的实现 32 | UserMapper userMapper = session.getMapper(UserMapper.class); 33 | // 组建查询参数 34 | User userParam = new User(); 35 | userParam.setSchoolName("Sunny School"); 36 | // 调用接口展开数据库操作 37 | List userList = userMapper.queryUserBySchoolName(userParam); 38 | // 打印查询结果 39 | for (User user : userList) { 40 | System.out.println("name : " + user.getName() + " ; email : " + user.getEmail()); 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /示例3/src/main/java/com/github/yeecode/mybatisdemo/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class User { 4 | private Integer id; 5 | private String name; 6 | private String email; 7 | private Integer age; 8 | private Integer sex; 9 | private String schoolName; 10 | 11 | 12 | public User() { 13 | } 14 | 15 | public User(String name, String email, Integer age, Integer sex, String schoolName) { 16 | this.name = name; 17 | this.email = email; 18 | this.age = age; 19 | this.sex = sex; 20 | this.schoolName = schoolName; 21 | } 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId(Integer id) { 28 | this.id = id; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public String getEmail() { 40 | return email; 41 | } 42 | 43 | public void setEmail(String email) { 44 | this.email = email; 45 | } 46 | 47 | public Integer getAge() { 48 | return age; 49 | } 50 | 51 | public void setAge(Integer age) { 52 | this.age = age; 53 | } 54 | 55 | public Integer getSex() { 56 | return sex; 57 | } 58 | 59 | public void setSex(Integer sex) { 60 | this.sex = sex; 61 | } 62 | 63 | public String getSchoolName() { 64 | return schoolName; 65 | } 66 | 67 | public void setSchoolName(String schoolName) { 68 | this.schoolName = schoolName; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /示例3/src/main/java/com/github/yeecode/mybatisdemo/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import java.util.List; 4 | 5 | public interface UserMapper { 6 | List queryUserBySchoolName(User user); 7 | } 8 | -------------------------------------------------------------------------------- /示例3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例3/src/main/resources/com/github/yeecode/mybatisdemo/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | -------------------------------------------------------------------------------- /示例3/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /示例4/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /示例4/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | 5 | @SpringBootApplication 6 | public class DemoApplication { 7 | public static void main(String[] args) { 8 | System.out.println("--原有Phone无录音功能--"); 9 | Phone phone = new TelePhone(); 10 | phone.callOut("Hello, this is yee."); 11 | 12 | System.out.println(); 13 | 14 | System.out.println("--经过装饰后的Phone有录音功能--"); 15 | Phone phoneWithRecorder = new PhoneRecordDecorator(phone); 16 | phoneWithRecorder.callOut("Hello, this is yee."); 17 | 18 | 19 | // 有短信功能的Phone 20 | Phone phoneWithMessage = new PhoneMessageDecorator(phone); 21 | ((PhoneMessageDecorator) phoneWithMessage).sendMessage("Hello, this is yee."); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /示例4/src/main/java/com/github/yeecode/mybatisdemo/Phone.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public interface Phone { 4 | String callIn(); 5 | Boolean callOut(String info); 6 | } 7 | -------------------------------------------------------------------------------- /示例4/src/main/java/com/github/yeecode/mybatisdemo/PhoneMessageDecorator.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class PhoneMessageDecorator implements Phone { 4 | private Phone decoratedPhone; 5 | 6 | public PhoneMessageDecorator(Phone decoratedPhone) { 7 | this.decoratedPhone = decoratedPhone; 8 | } 9 | 10 | @Override 11 | public String callIn() { 12 | return decoratedPhone.callIn(); 13 | } 14 | 15 | @Override 16 | public Boolean callOut(String info) { 17 | return decoratedPhone.callOut(info); 18 | } 19 | 20 | public String receiveMessage() { 21 | // 省略接受短信操作 22 | return "receive message"; 23 | } 24 | 25 | public Boolean sendMessage(String info) { 26 | // 省略发送短信操作 27 | return true; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /示例4/src/main/java/com/github/yeecode/mybatisdemo/PhoneRecordDecorator.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class PhoneRecordDecorator implements Phone { 4 | private Phone decoratedPhone; 5 | 6 | public PhoneRecordDecorator(Phone decoratedPhone) { 7 | this.decoratedPhone = decoratedPhone; 8 | } 9 | 10 | @Override 11 | public String callIn() { 12 | System.out.println("启动录音……"); 13 | String info = decoratedPhone.callIn(); 14 | System.out.println("结束录音并保存录音文件。"); 15 | return info; 16 | } 17 | 18 | @Override 19 | public Boolean callOut(String info) { 20 | System.out.println("启动录音……"); 21 | Boolean result = decoratedPhone.callOut(info); 22 | System.out.println("结束录音并保存录音文件。"); 23 | return result; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /示例4/src/main/java/com/github/yeecode/mybatisdemo/TelePhone.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class TelePhone implements Phone { 4 | @Override 5 | public String callIn() { 6 | System.out.println("接受语音……"); 7 | return "get info"; 8 | } 9 | 10 | @Override 11 | public Boolean callOut(String info) { 12 | System.out.println("发送语音:" + info); 13 | return true; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /示例4/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例5/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /示例5/src/main/java/com/github/yeecode/mybatisdemo/Book.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class Book { 4 | private String bookName; 5 | private Double price; 6 | 7 | public Book(String bookName, Double price) { 8 | this.bookName = bookName; 9 | this.price = price; 10 | } 11 | 12 | public String getBookName() { 13 | return bookName; 14 | } 15 | 16 | public void setBookName(String bookName) { 17 | this.bookName = bookName; 18 | } 19 | 20 | public Double getPrice() { 21 | return price; 22 | } 23 | 24 | public void setPrice(Double price) { 25 | this.price = price; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /示例5/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | 5 | import java.lang.reflect.Field; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | @SpringBootApplication 10 | public class DemoApplication { 11 | public static void main(String[] args) { 12 | User oldUser = new User(1,"yee"); 13 | User newUser = new User(1,"yeecode"); 14 | 15 | System.out.println("不使用反射,只能比较单一类型的对象:"); 16 | 17 | Map diffUserMap = diffUser(oldUser,newUser); 18 | for (Map.Entry entry : diffUserMap.entrySet()) { 19 | System.out.println("属性" + entry.getKey() + "; 变化为:" + entry.getValue()); 20 | } 21 | 22 | System.out.println("使用反射,可以比较属性不同的各类对象:"); 23 | 24 | Map diffObjMap = diffObj(oldUser,newUser); 25 | for (Map.Entry entry : diffObjMap.entrySet()) { 26 | System.out.println("属性" + entry.getKey() + "; 变化为:" + entry.getValue()); 27 | } 28 | 29 | Book oldBook = new Book("语文",15.7); 30 | Book newBook = new Book("语文",18.7); 31 | diffObjMap = diffObj(oldBook,newBook); 32 | for (Map.Entry entry : diffObjMap.entrySet()) { 33 | System.out.println("属性" + entry.getKey() + "; 变化为:" + entry.getValue()); 34 | } 35 | } 36 | 37 | /** 38 | * 比较两个User对象的属性不同 39 | * @param oldUser 第一个User对象 40 | * @param newUser 第二个User对象 41 | * @return 两个User对象的属性变化 42 | */ 43 | public static Map diffUser(User oldUser, User newUser) { 44 | Map diffMap = new HashMap<>(); 45 | if ((oldUser.getId() == null && newUser.getId() != null) || 46 | (oldUser.getId()!= null && !oldUser.getId().equals(newUser.getId()))) 47 | { 48 | diffMap.put("id","from " + oldUser.getId() + " to " + newUser.getId()); 49 | } 50 | if ((oldUser.getName() == null && newUser.getName() != null) || 51 | (oldUser.getName()!= null && !oldUser.getName().equals(newUser.getName()))) 52 | { 53 | diffMap.put("name","from " + oldUser.getName() + " to " + newUser.getName()); 54 | } 55 | return diffMap; 56 | } 57 | 58 | /** 59 | * 比较两个任意对象的属性不同 60 | * @param oldObj 第一个对象 61 | * @param newObj 第二个对象 62 | * @return 两个对象的属性变化 63 | */ 64 | public static Map diffObj(Object oldObj, Object newObj) { 65 | Map diffMap = new HashMap<>(); 66 | try { 67 | // 获取对象的类 68 | Class oldObjClazz = oldObj.getClass(); 69 | Class newObjClazz = newObj.getClass(); 70 | // 判断两个对象是否属于同一个类 71 | if (oldObjClazz.equals(newObjClazz)) { 72 | // 获取对象的所有属性 73 | Field[] fields = oldObjClazz.getDeclaredFields(); 74 | // 对每个属性逐一判断 75 | for (Field field : fields) { 76 | // 使得属性可以被反射访问 77 | field.setAccessible(true); 78 | // 拿到当前属性的值 79 | Object oldValue = field.get(oldObj); 80 | Object newValue = field.get(newObj); 81 | // 如果某个属性的值在两个对象中不同,则进行记录 82 | if ((oldValue == null && newValue != null) || oldValue != null && !oldValue.equals(newValue)) { 83 | diffMap.put(field.getName(), "from " + oldValue + " to " + newValue); 84 | } 85 | } 86 | } 87 | } catch (Exception ex) { 88 | ex.printStackTrace(); 89 | } 90 | return diffMap; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /示例5/src/main/java/com/github/yeecode/mybatisdemo/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class User { 4 | private Integer id; 5 | private String name; 6 | 7 | public User() { 8 | } 9 | 10 | public User(Integer id, String name) { 11 | this.id = id; 12 | this.name = name; 13 | } 14 | 15 | public Integer getId() { 16 | return id; 17 | } 18 | 19 | public void setId(Integer id) { 20 | this.id = id; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public void setName(String name) { 28 | this.name = name; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /示例5/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例5/src/main/resources/com/github/yeecode/mybatisdemo/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | -------------------------------------------------------------------------------- /示例6/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | mysql 28 | mysql-connector-java 29 | 8.0.17 30 | 31 | 32 | org.mybatis 33 | mybatis 34 | 3.5.2 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-maven-plugin 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /示例6/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.apache.ibatis.reflection.TypeParameterResolver; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | import java.lang.reflect.Type; 7 | 8 | @SpringBootApplication 9 | public class DemoApplication { 10 | public static void main(String[] args) { 11 | try { 12 | // 使用TypeParameterResolver分析User类中getInfo方法输出结果的具体类型 13 | Type type1 = TypeParameterResolver.resolveReturnType(User.class.getMethod("getInfo"), User.class); 14 | System.out.println("User类中getInfo方法的输出结果类型 :\n" + type1); 15 | 16 | // 使用TypeParameterResolver分析Student类中getInfo方法输出结果的具体类型 17 | Type type2 = TypeParameterResolver.resolveReturnType(User.class.getMethod("getInfo"), Student.class); 18 | System.out.println("Student类中getInfo方法的输出结果类型 :\n" + type2); 19 | } catch (Exception ex) { 20 | ex.printStackTrace(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /示例6/src/main/java/com/github/yeecode/mybatisdemo/Student.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | public class Student extends User { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /示例6/src/main/java/com/github/yeecode/mybatisdemo/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import java.util.List; 4 | 5 | public class User { 6 | public List getInfo() { 7 | return null; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /示例6/src/main/java/com/github/yeecode/mybatisdemo/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import java.util.List; 4 | 5 | public interface UserMapper { 6 | List queryUserBySchoolName(User user); 7 | } 8 | -------------------------------------------------------------------------------- /示例6/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例6/src/main/resources/com/github/yeecode/mybatisdemo/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | -------------------------------------------------------------------------------- /示例6/src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /示例7/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-maven-plugin 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /示例7/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.WipeGlass; 4 | import com.github.yeecode.mybatisdemo.model.Cleaning; 5 | import com.github.yeecode.mybatisdemo.model.WipeBlackboard; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | @SpringBootApplication 9 | public class DemoApplication { 10 | public static void main(String[] args) { 11 | Cleaning wipeGlass = new WipeGlass(); 12 | wipeGlass.clean(); 13 | 14 | Cleaning wipeBlackboard = new WipeBlackboard(); 15 | wipeBlackboard.clean(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /示例7/src/main/java/com/github/yeecode/mybatisdemo/model/Cleaning.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public abstract class Cleaning { 4 | 5 | public void clean(){ 6 | prepare(); 7 | implement(); 8 | windup(); 9 | report(); 10 | } 11 | 12 | abstract void prepare(); 13 | 14 | abstract void implement(); 15 | 16 | abstract void windup(); 17 | 18 | void report() { 19 | System.out.println("告诉别人已经打扫完成。"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /示例7/src/main/java/com/github/yeecode/mybatisdemo/model/WipeBlackboard.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class WipeBlackboard extends Cleaning { 4 | @Override 5 | void prepare() { 6 | System.out.println("找到黑板檫。"); 7 | } 8 | 9 | @Override 10 | void implement() { 11 | System.out.println("用力擦黑板。"); 12 | } 13 | 14 | @Override 15 | void windup() { 16 | System.out.println("清理粉笔屑。"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /示例7/src/main/java/com/github/yeecode/mybatisdemo/model/WipeGlass.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class WipeGlass extends Cleaning{ 4 | @Override 5 | void prepare() { 6 | System.out.println("找到抹布。"); 7 | System.out.println("浸湿和清洗抹布。"); 8 | } 9 | 10 | @Override 11 | void implement() { 12 | System.out.println("擦玻璃。"); 13 | } 14 | 15 | @Override 16 | void windup() { 17 | System.out.println("清理窗台。"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /示例7/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例7/src/test/java/com/github/yeecode/mybatisdemo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /示例8/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-maven-plugin 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /示例8/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.UserProxy; 4 | import com.github.yeecode.mybatisdemo.model.User; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | public class DemoApplication { 9 | public static void main(String[] args) throws Exception { 10 | // 生成被代理对象 11 | User user = new User(); 12 | 13 | // 生成代理,顺便告诉代理它要代理谁 14 | UserProxy userProxy = new UserProxy(user); 15 | 16 | // 触发代理方法 17 | userProxy.sayHello("易哥"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /示例8/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class User implements UserInterface { 4 | @Override 5 | public String sayHello(String name) { 6 | System.out.println("hello " + name); 7 | return "OK"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /示例8/src/main/java/com/github/yeecode/mybatisdemo/model/UserInterface.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public interface UserInterface { 4 | String sayHello(String name); 5 | } 6 | -------------------------------------------------------------------------------- /示例8/src/main/java/com/github/yeecode/mybatisdemo/model/UserProxy.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class UserProxy implements UserInterface { 4 | private UserInterface target; 5 | 6 | public UserProxy(UserInterface target) { 7 | this.target = target; 8 | } 9 | 10 | @Override 11 | public String sayHello(String name) { 12 | System.out.println("pre words"); 13 | target.sayHello(name); 14 | System.out.println("post words"); 15 | return name; 16 | } 17 | } -------------------------------------------------------------------------------- /示例8/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例8/src/test/java/com/github/yeecode/mybatisdemo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /示例9/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.github.yeecode.mybatisdemo 12 | demo 13 | 0.0.1-SNAPSHOT 14 | demo 15 | Demo project for Spring Boot 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-maven-plugin 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /示例9/src/main/java/com/github/yeecode/mybatisdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import com.github.yeecode.mybatisdemo.model.ProxyHandler; 4 | import com.github.yeecode.mybatisdemo.model.User; 5 | import com.github.yeecode.mybatisdemo.model.UserInterface; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | import java.lang.reflect.Proxy; 9 | import java.sql.Connection; 10 | import java.sql.DriverManager; 11 | import java.sql.ResultSet; 12 | import java.sql.Statement; 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | @SpringBootApplication 17 | public class DemoApplication { 18 | public static void main(String[] args) throws Exception { 19 | // 创建被代理对象 20 | User user = new User(); 21 | // 初始化一个ProxyHandler对象 22 | ProxyHandler proxyHandler = new ProxyHandler(user); 23 | 24 | // 使用Proxy类的一个静态方法生成代理对象userProxy 25 | UserInterface userProxy = 26 | (UserInterface) Proxy.newProxyInstance( 27 | User.class.getClassLoader(), 28 | new Class[] { UserInterface.class }, 29 | proxyHandler); 30 | 31 | // 通过接口调用相应的方法,实际由Proxy执行 32 | userProxy.sayHello("易哥"); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /示例9/src/main/java/com/github/yeecode/mybatisdemo/model/ProxyHandler.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | import java.lang.reflect.InvocationHandler; 4 | import java.lang.reflect.Method; 5 | 6 | public class ProxyHandler implements InvocationHandler { 7 | private T target; 8 | 9 | public ProxyHandler(T target) { 10 | this.target = target; 11 | } 12 | 13 | /** 14 | * @param proxy 被代理的对象 15 | * @param method 要调用的方法 16 | * @param args 方法调用时所需要参数 17 | * @return 18 | * @throws Throwable 19 | */ 20 | @Override 21 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 22 | System.out.println("pre words"); 23 | Object ans = method.invoke(target, args); 24 | System.out.println("post words"); 25 | return ans; 26 | } 27 | } -------------------------------------------------------------------------------- /示例9/src/main/java/com/github/yeecode/mybatisdemo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public class User implements UserInterface { 4 | @Override 5 | public String sayHello(String name) { 6 | System.out.println("hello " + name); 7 | return "OK"; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /示例9/src/main/java/com/github/yeecode/mybatisdemo/model/UserInterface.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo.model; 2 | 3 | public interface UserInterface { 4 | String sayHello(String name); 5 | } 6 | -------------------------------------------------------------------------------- /示例9/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /示例9/src/test/java/com/github/yeecode/mybatisdemo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.github.yeecode.mybatisdemo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class DemoApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------