├── README.md ├── pom.xml ├── springboot-integ-email ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── lm │ │ │ ├── App.java │ │ │ └── service │ │ │ ├── MailService.java │ │ │ └── impl │ │ │ └── MailServiceImpl.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ └── emailTemplate.html │ └── test │ └── java │ └── com │ └── lm │ ├── AppTest.java │ └── MailServiceTest.java ├── springboot-integ-jpa ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── lm │ │ │ ├── App.java │ │ │ ├── controller │ │ │ ├── BaseController.java │ │ │ └── UserController.java │ │ │ ├── entity │ │ │ └── User.java │ │ │ ├── repository │ │ │ └── UserRepository.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── UserServiceImpl.java │ │ │ ├── utils │ │ │ └── Constant.java │ │ │ └── vo │ │ │ └── UserVo.java │ └── resources │ │ ├── application.properties │ │ └── db.sql │ └── test │ └── java │ └── com │ └── lm │ ├── AppTest.java │ └── UserTest.java ├── springboot-integ-mongo-mdsource ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── lm │ │ │ ├── first │ │ │ ├── App.java │ │ │ ├── config │ │ │ │ ├── AbstractMongoConfig.java │ │ │ │ ├── PrimaryMongoConfig.java │ │ │ │ └── SecondaryMongoConfig.java │ │ │ ├── dao │ │ │ │ ├── MongoObjectDao.java │ │ │ │ └── MongoObjectDaoImpl.java │ │ │ └── entry │ │ │ │ ├── PrimaryMongoObject.java │ │ │ │ └── SecondaryMongoObject.java │ │ │ ├── lombok │ │ │ └── entity │ │ │ │ └── Test.java │ │ │ └── second │ │ │ ├── App.java │ │ │ ├── config │ │ │ ├── MultipleMongoConfig.java │ │ │ ├── PrimaryMongoConfig.java │ │ │ ├── SecondaryMongoConfig.java │ │ │ └── props │ │ │ │ └── MultipleMongoProperties.java │ │ │ ├── dao │ │ │ ├── SecondMongoObjectDao.java │ │ │ └── SecondMongoObjectDaoImpl.java │ │ │ ├── entity │ │ │ ├── PrimaryMongoObject.java │ │ │ └── SecondaryMongoObject.java │ │ │ └── repository │ │ │ ├── primary │ │ │ └── PrimaryRepository.java │ │ │ └── secondary │ │ │ └── SecondaryRepository.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── lm │ ├── first │ ├── AppTest.java │ └── MongoDaoTest.java │ └── second │ └── SecondMongoDaoTest.java ├── springboot-integ-mongo ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── lm │ │ │ ├── App.java │ │ │ ├── dao │ │ │ ├── UserDao.java │ │ │ └── impl │ │ │ │ └── UserDaoImpl.java │ │ │ └── entity │ │ │ └── UserEntity.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── lm │ ├── AppTest.java │ └── UserDaoTest.java ├── springboot-integ-mybatis-annotation-mdsource ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── lm │ │ │ ├── App.java │ │ │ ├── datasource │ │ │ ├── DataSource1Config.java │ │ │ └── DataSource2Config.java │ │ │ ├── entity │ │ │ └── UserEntity.java │ │ │ ├── enums │ │ │ └── SexEnums.java │ │ │ ├── mapper │ │ │ ├── test1 │ │ │ │ └── User1Mapper.java │ │ │ └── test2 │ │ │ │ └── User2Mapper.java │ │ │ └── web │ │ │ └── UserController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── lm │ └── AppTest.java ├── springboot-integ-mybatis-annotation ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── lm │ │ │ ├── App.java │ │ │ ├── entity │ │ │ └── UserEntity.java │ │ │ ├── enums │ │ │ └── SexEnums.java │ │ │ ├── mapper │ │ │ └── UserMapper.java │ │ │ └── web │ │ │ └── UserController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── lm │ ├── AppTest.java │ └── UserMapperTest.java ├── springboot-integ-mybatis-xml-mdsource ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── lm │ │ │ ├── App.java │ │ │ ├── datasource │ │ │ ├── DataSource1Config.java │ │ │ └── DataSource2Config.java │ │ │ ├── entity │ │ │ └── UserEntity.java │ │ │ ├── enums │ │ │ └── SexEnums.java │ │ │ ├── mapper │ │ │ ├── test1 │ │ │ │ └── User1Mapper.java │ │ │ └── test2 │ │ │ │ └── User2Mapper.java │ │ │ └── web │ │ │ └── UserController.java │ └── resources │ │ ├── application.properties │ │ ├── mybatis │ │ ├── mapper │ │ │ ├── test1 │ │ │ │ └── UserMapper.xml │ │ │ └── test2 │ │ │ │ └── UserMapper.xml │ │ └── mybatis-config.xml │ │ └── user.sql │ └── test │ └── java │ └── com │ └── lm │ ├── AppTest.java │ ├── User1MapperTest.java │ └── User2MapperTest.java ├── springboot-integ-mybatis-xml ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── lm │ │ │ ├── App.java │ │ │ ├── entity │ │ │ └── UserEntity.java │ │ │ ├── enums │ │ │ └── SexEnums.java │ │ │ ├── mapper │ │ │ └── UserMapper.java │ │ │ └── web │ │ │ └── UserController.java │ └── resources │ │ ├── application.properties │ │ ├── mybatis │ │ ├── mapper │ │ │ └── UserMapper.xml │ │ └── mybatis-config.xml │ │ └── user.sql │ └── test │ └── java │ └── com │ └── lm │ ├── AppTest.java │ └── UserMapperTest.java └── springboot-integ-redis ├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── pom.xml └── src └── main ├── java └── com │ └── lm │ ├── App.java │ ├── config │ └── RedisCacheConfig.java │ ├── controller │ ├── BaseController.java │ ├── CacheRedisController.java │ └── UserController.java │ ├── entity │ └── User.java │ ├── repository │ └── UserRepository.java │ ├── service │ ├── UserService.java │ └── UserServiceImpl.java │ └── util │ └── Constant.java └── resources └── application.properties /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-integration 2 | spring-boot集成实例 mybatis,mail,mysql,mongodb,cassandra,scheduler,redis,kafka,shiro,websocket 3 | 1.集成单数据源mybatis 4 | 2.集成多数据源mybatis 5 | 3.集成邮件mail 6 | 4.集成mongodb 7 | 5.集成cassandra 8 | 6.集成定时任务scheduler 9 | 7.集成redis 10 | 8.集成kafka 11 | 9.集成shiro 12 | 10.集成websocket 13 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | com.lm 6 | spring-boot-integration 7 | 0.0.1-SNAPSHOT 8 | pom 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 1.5.0.RELEASE 14 | 15 | 16 | 17 | 18 | UTF-8 19 | 1.8 20 | 21 | 22 | 23 | springboot-integ-mybatis-xml 24 | springboot-integ-mybatis-xml-mdsource 25 | springboot-integ-mybatis-annotation 26 | springboot-integ-mybatis-annotation-mdsource 27 | springboot-integ-email 28 | 29 | springboot-integ-mongo 30 | springboot-integ-mongo-mdsource 31 | springboot-integ-redis 32 | springboot-integ-jpa 33 | 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-compiler-plugin 39 | 40 | ${java.version} 41 | ${java.version} 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 49 | true 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /springboot-integ-email/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.lm 8 | spring-boot-integration 9 | 0.0.1-SNAPSHOT 10 | 11 | springboot-integ-email 12 | springboot-integ-email 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-test 25 | test 26 | 27 | 28 | org.springframework 29 | spring-context-support 30 | 31 | 32 | 33 | com.sun.mail 34 | javax.mail 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-thymeleaf 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | true 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /springboot-integ-email/src/main/java/com/lm/App.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * springboot入口 8 | * 9 | * @author liangming.deng 10 | * @date 2017年7月29日 11 | * 12 | */ 13 | @SpringBootApplication 14 | public class App { 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(App.class, args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /springboot-integ-email/src/main/java/com/lm/service/MailService.java: -------------------------------------------------------------------------------- 1 | package com.lm.service; 2 | 3 | public interface MailService { 4 | /** 5 | * 发送普通邮件 6 | * @param to 7 | * @param subject 8 | * @param content 9 | */ 10 | public void sendSimpleMail(String to, String subject, String content); 11 | 12 | /** 13 | * 发送html格式邮件 14 | * @param to 15 | * @param subject 16 | * @param content 17 | */ 18 | public void sendHtmlMail(String to, String subject, String content); 19 | 20 | /** 21 | * 发送带附件邮件 22 | * @param to 23 | * @param subject 24 | * @param content 25 | * @param filePath 26 | */ 27 | public void sendAttachmentsMail(String to, String subject, String content, String filePath); 28 | 29 | /** 30 | * 发送正文中有静态资源(图片)的邮件 31 | * @param to 32 | * @param subject 33 | * @param content 34 | * @param rscPath 35 | * @param rscId 36 | */ 37 | public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId); 38 | } 39 | -------------------------------------------------------------------------------- /springboot-integ-email/src/main/java/com/lm/service/impl/MailServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.lm.service.impl; 2 | 3 | import java.io.File; 4 | 5 | import javax.mail.MessagingException; 6 | import javax.mail.internet.MimeMessage; 7 | 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.beans.factory.annotation.Value; 12 | import org.springframework.core.io.FileSystemResource; 13 | import org.springframework.mail.SimpleMailMessage; 14 | import org.springframework.mail.javamail.JavaMailSender; 15 | import org.springframework.mail.javamail.MimeMessageHelper; 16 | import org.springframework.stereotype.Component; 17 | 18 | import com.lm.service.MailService; 19 | 20 | /** 21 | * 邮件实现类 22 | * @author liangming.deng 23 | * @date 2017年7月29日 24 | * 25 | */ 26 | @Component 27 | public class MailServiceImpl implements MailService{ 28 | 29 | private final Logger logger = LoggerFactory.getLogger(this.getClass()); 30 | 31 | @Value("${mail.fromMail.addr}") 32 | private String from; 33 | 34 | @Autowired 35 | private JavaMailSender mailSender; 36 | 37 | /** 38 | * 发送文本邮件 39 | * @param to 40 | * @param subject 41 | * @param content 42 | */ 43 | @Override 44 | public void sendSimpleMail(String to, String subject, String content) { 45 | SimpleMailMessage message = new SimpleMailMessage(); 46 | message.setFrom(from); 47 | message.setTo(to); 48 | message.setSubject(subject); 49 | message.setText(content); 50 | 51 | try { 52 | mailSender.send(message); 53 | logger.info("简单邮件已经发送。"); 54 | } catch (Exception e) { 55 | logger.error("发送简单邮件时发生异常!", e); 56 | } 57 | 58 | } 59 | 60 | /** 61 | * 发送html邮件 62 | * @param to 63 | * @param subject 64 | * @param content 65 | */ 66 | @Override 67 | public void sendHtmlMail(String to, String subject, String content) { 68 | MimeMessage message = mailSender.createMimeMessage(); 69 | 70 | try { 71 | //true表示需要创建一个multipart message 72 | MimeMessageHelper helper = new MimeMessageHelper(message, true); 73 | helper.setFrom(from); 74 | helper.setTo(to); 75 | helper.setSubject(subject); 76 | helper.setText(content, true); 77 | 78 | mailSender.send(message); 79 | logger.info("html邮件发送成功"); 80 | } catch (MessagingException e) { 81 | logger.error("发送html邮件时发生异常!", e); 82 | } 83 | } 84 | 85 | 86 | /** 87 | * 发送带附件的邮件 88 | * @param to 89 | * @param subject 90 | * @param content 91 | * @param filePath 92 | */ 93 | public void sendAttachmentsMail(String to, String subject, String content, String filePath){ 94 | MimeMessage message = mailSender.createMimeMessage(); 95 | 96 | try { 97 | MimeMessageHelper helper = new MimeMessageHelper(message, true); 98 | helper.setFrom(from); 99 | helper.setTo(to); 100 | helper.setSubject(subject); 101 | helper.setText(content, true); 102 | 103 | FileSystemResource file = new FileSystemResource(new File(filePath)); 104 | String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); 105 | helper.addAttachment(fileName, file); 106 | //helper.addAttachment("test"+fileName, file); 107 | 108 | mailSender.send(message); 109 | logger.info("带附件的邮件已经发送。"); 110 | } catch (MessagingException e) { 111 | logger.error("发送带附件的邮件时发生异常!", e); 112 | } 113 | } 114 | 115 | 116 | /** 117 | * 发送正文中有静态资源(图片)的邮件 118 | * @param to 119 | * @param subject 120 | * @param content 121 | * @param rscPath 122 | * @param rscId 123 | */ 124 | public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){ 125 | MimeMessage message = mailSender.createMimeMessage(); 126 | 127 | try { 128 | MimeMessageHelper helper = new MimeMessageHelper(message, true); 129 | helper.setFrom(from); 130 | helper.setTo(to); 131 | helper.setSubject(subject); 132 | helper.setText(content, true); 133 | 134 | FileSystemResource res = new FileSystemResource(new File(rscPath)); 135 | helper.addInline(rscId, res); 136 | 137 | mailSender.send(message); 138 | logger.info("嵌入静态资源的邮件已经发送。"); 139 | } catch (MessagingException e) { 140 | logger.error("发送嵌入静态资源的邮件时发生异常!", e); 141 | } 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /springboot-integ-email/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=spirng-boot-email 2 | 3 | #SMTP server host. For instance `smtp.example.com` 4 | spring.mail.host=smtp.163.com 5 | #Login user of the SMTP server 6 | spring.mail.username=email_test_123@163.com 7 | #Login password of the SMTP server 8 | #此处为客户端授权密码EMAIL123 9 | spring.mail.password=EMAIL123 10 | #Default MimeMessage encoding 11 | spring.mail.default-encoding=UTF-8 12 | 13 | #发送邮箱地址 14 | mail.fromMail.addr=email_test_123@163.com 15 | 16 | 17 | -------------------------------------------------------------------------------- /springboot-integ-email/src/main/resources/templates/emailTemplate.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Title 6 | 7 | 8 | 您好,这是验证邮件,请点击下面的链接完成验证,
9 | 激活账号 10 | 11 | -------------------------------------------------------------------------------- /springboot-integ-email/src/test/java/com/lm/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /springboot-integ-email/src/test/java/com/lm/MailServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | import org.thymeleaf.TemplateEngine; 9 | import org.thymeleaf.context.Context; 10 | 11 | import com.lm.service.MailService; 12 | 13 | /** 14 | * 15 | * @author liangming.deng 16 | * @date 2017年7月29日 17 | * 18 | */ 19 | @RunWith(SpringRunner.class) 20 | @SpringBootTest 21 | public class MailServiceTest { 22 | 23 | @Autowired 24 | private MailService mailService; 25 | 26 | @Autowired 27 | private TemplateEngine templateEngine; 28 | 29 | @Test 30 | public void testSimpleMail() throws Exception { 31 | mailService.sendSimpleMail("email_test_123@163.com","test simple mail"," hello this is simple mail"); 32 | } 33 | 34 | @Test 35 | public void testHtmlMail() throws Exception { 36 | String content="\n" + 37 | "\n" + 38 | "

hello world ! 这是一封html邮件!

\n" + 39 | "\n" + 40 | ""; 41 | mailService.sendHtmlMail("email_test_123@163.com","test simple mail",content); 42 | } 43 | 44 | @Test 45 | public void sendAttachmentsMail() { 46 | String filePath="d:\\tmp\\iot-springboot.log"; 47 | mailService.sendAttachmentsMail("email_test_123@163.com", "主题:带附件的邮件", "有附件,请查收!", filePath); 48 | } 49 | 50 | 51 | @Test 52 | public void sendInlineResourceMail() { 53 | String rscId = "test006"; 54 | String content="这是有图片的邮件:"; 55 | String imgPath = "D:\\01.jpg"; 56 | 57 | mailService.sendInlineResourceMail("email_test_123@163.com", "主题:这是有图片的邮件", content, imgPath, rscId); 58 | } 59 | 60 | 61 | @Test 62 | public void sendTemplateMail() { 63 | //创建邮件正文 64 | Context context = new Context(); 65 | context.setVariable("id", "006"); 66 | String emailContent = templateEngine.process("emailTemplate", context); 67 | 68 | mailService.sendHtmlMail("email_test_123@163.com","主题:这是模板邮件",emailContent); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /springboot-integ-jpa/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.lm 8 | spring-boot-integration 9 | 0.0.1-SNAPSHOT 10 | 11 | com.lm 12 | springboot-integ-jpa 13 | 0.0.1-SNAPSHOT 14 | springboot-integ-jpa 15 | http://maven.apache.org 16 | 17 | UTF-8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-devtools 27 | 28 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-jdbc 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-aop 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-data-jpa 50 | 51 | 52 | 53 | mysql 54 | mysql-connector-java 55 | 56 | 57 | 58 | junit 59 | junit 60 | test 61 | 62 | 63 | org.projectlombok 64 | lombok 65 | 66 | 67 | 68 | com.alibaba 69 | druid 70 | 1.0.11 71 | 72 | 73 | 74 | 75 | net.sf.json-lib 76 | json-lib 77 | 0.9 78 | 79 | 80 | javax.servlet 81 | servlet-api 82 | 83 | 84 | 85 | 86 | org.apache.httpcomponents 87 | httpclient 88 | 4.5.3 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/java/com/lm/App.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * springboot启动 8 | * @author liangming.deng 9 | * @date 2017年11月20日 10 | * 11 | */ 12 | @SpringBootApplication 13 | public class App { 14 | public static void main(String[] args) { 15 | SpringApplication.run(App.class, args); 16 | } 17 | } -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/java/com/lm/controller/BaseController.java: -------------------------------------------------------------------------------- 1 | package com.lm.controller; 2 | 3 | import org.springframework.ui.ModelMap; 4 | 5 | /** 6 | * controller基类 7 | * @author liangming.deng 8 | * @date 2017年11月20日 9 | * 10 | */ 11 | public class BaseController { 12 | 13 | /** 14 | * 接口返回通用规范 15 | * @param code 16 | * @param msg 17 | * @param data 18 | * @return 19 | */ 20 | public ModelMap result(String code, String msg, Object data) { 21 | ModelMap modelMap = new ModelMap(); 22 | modelMap.put("code", code); 23 | modelMap.put("msg", msg); 24 | modelMap.put("result", data); 25 | return modelMap; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/java/com/lm/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.lm.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.ui.ModelMap; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import com.lm.entity.User; 13 | import com.lm.service.UserService; 14 | import com.lm.utils.Constant; 15 | 16 | /** 17 | * 18 | * @author liangming.deng 19 | * @date 2017年11月20日 20 | * 21 | */ 22 | @RestController 23 | @RequestMapping("/user") 24 | public class UserController extends BaseController { 25 | 26 | @Autowired 27 | private UserService userService; 28 | 29 | 30 | @RequestMapping(value = "/selectGet/{id}", method = RequestMethod.GET) 31 | public ModelMap selectGet(@PathVariable Integer id) { 32 | User user = userService.selectOne(id); 33 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, user); 34 | } 35 | 36 | @RequestMapping(value = "/selectAll", method = RequestMethod.GET) 37 | public ModelMap search() { 38 | List userList = userService.selectAll(); 39 | 40 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, userList); 41 | } 42 | 43 | @RequestMapping(value = "/selectPost", method = RequestMethod.POST) 44 | public ModelMap selectPost(String username, String password) { 45 | List userList = userService.selectPost(username, password); 46 | 47 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, userList); 48 | } 49 | 50 | @RequestMapping(value = "/selectPut", method = RequestMethod.PUT) 51 | public ModelMap selectPut(Integer id,String username, String password) { 52 | List userList = userService.selectPost(username, password); 53 | 54 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, userList); 55 | } 56 | 57 | @RequestMapping(value = "/selectDelete/{id}", method = RequestMethod.DELETE) 58 | public ModelMap selectDelete(@PathVariable Integer id) { 59 | 60 | userService.selectDelete(id); 61 | 62 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, 0); 63 | } 64 | 65 | @RequestMapping(value = "/selectHead/{id}", method = RequestMethod.HEAD) 66 | public ModelMap selectHead(@PathVariable Integer id) { 67 | User user = userService.selectOne(id); 68 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, user); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/java/com/lm/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.lm.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | 5 | import lombok.AllArgsConstructor; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | import lombok.ToString; 9 | 10 | import javax.persistence.*; 11 | import java.io.Serializable; 12 | import java.util.Date; 13 | 14 | /** 15 | * lombok 16 | * @author liangming.deng 17 | * @date 2017年11月20日 18 | * 19 | */ 20 | 21 | @Data 22 | @NoArgsConstructor 23 | @AllArgsConstructor 24 | @ToString 25 | @Entity 26 | @Table(name = "t_user") 27 | public class User implements Serializable { 28 | /** 29 | * 30 | */ 31 | private static final long serialVersionUID = 1L; 32 | 33 | @Id 34 | @GeneratedValue(strategy = GenerationType.IDENTITY) 35 | private Integer id; 36 | 37 | @Column(name = "username") 38 | private String username; 39 | 40 | @Column(name = "password") 41 | private String password; 42 | 43 | @Column(name = "create_user") 44 | private String createUser; 45 | 46 | @Column(name = "create_time") 47 | @JsonFormat(pattern = "yyyy-mm-dd HH:mm:ss") 48 | private Date createTime; 49 | 50 | @Column(name = "update_user") 51 | private String updateUser; 52 | 53 | @Column(name = "update_time") 54 | @JsonFormat(pattern = "yyyy-mm-dd HH:mm:ss") 55 | private Date updateTime; 56 | 57 | public User(String username, String password, String createUser, Date createTime, String updateUser, Date updateTime) { 58 | this.username = username; 59 | this.password = password; 60 | this.createUser = createUser; 61 | this.createTime = createTime; 62 | this.updateUser = updateUser; 63 | this.updateTime = updateTime; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/java/com/lm/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.lm.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | import com.lm.entity.User; 8 | 9 | /** 10 | * 11 | * @author liangming.deng 12 | * @date 2017年11月20日 13 | * 14 | */ 15 | public interface UserRepository extends JpaRepository { 16 | List findByUsernameAndPassword(String username,String password); 17 | 18 | List findByUsernameNot(String username); 19 | } 20 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/java/com/lm/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.lm.service; 2 | 3 | import java.util.List; 4 | 5 | import com.lm.entity.User; 6 | 7 | /** 8 | * 9 | * @author liangming.deng 10 | * @date 2017年11月20日 11 | * 12 | */ 13 | public interface UserService { 14 | /** 15 | * 获取所有数据 16 | * @return 17 | */ 18 | List selectAll(); 19 | 20 | /** 21 | * 根据id获取数据 22 | * @param id 23 | * @return 24 | */ 25 | User selectOne(Integer id); 26 | 27 | /** 28 | * 根据id删除数据 29 | * @param id 30 | */ 31 | void selectDelete(Integer id); 32 | 33 | /** 34 | * 根据条件获取 35 | * @param username 36 | * @param password 37 | * @return 38 | */ 39 | List selectPost(String username,String password); 40 | 41 | /** 42 | * 根据id获取 43 | * @param id 44 | * @return 45 | */ 46 | User selectPut(Integer id,String username, String password); 47 | 48 | /** 49 | * 获取不包含username的数据 50 | * @param username 51 | * @return 52 | */ 53 | List selectUserNot(String username); 54 | } 55 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/java/com/lm/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.lm.service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Date; 5 | import java.util.List; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import com.lm.entity.User; 11 | import com.lm.repository.UserRepository; 12 | 13 | /** 14 | * 15 | * @author liangming.deng 16 | * @date 2017年11月20日 17 | * 18 | */ 19 | @Service 20 | public class UserServiceImpl implements UserService { 21 | @Autowired 22 | private UserRepository userRepository; 23 | 24 | 25 | public List selectAll() { 26 | return userRepository.findAll(); 27 | } 28 | 29 | @Override 30 | public User selectOne(Integer id) { 31 | return userRepository.findOne(id); 32 | } 33 | 34 | @Override 35 | public void selectDelete(Integer id) { 36 | userRepository.delete(id); 37 | } 38 | 39 | @Override 40 | public List selectPost(String username, String password) { 41 | User user = new User(username, password, username, new Date(), username, new Date()); 42 | List users = new ArrayList<>(); 43 | users.add(user); 44 | userRepository.save(users); 45 | return users; 46 | } 47 | 48 | @Override 49 | public User selectPut(Integer id,String username, String password) { 50 | User user = userRepository.getOne(id); 51 | if(user == null){ 52 | return user; 53 | } 54 | user.setPassword(password); 55 | user.setUsername(username); 56 | userRepository.save(user); 57 | return user; 58 | } 59 | 60 | @Override 61 | public List selectUserNot(String username) { 62 | 63 | return userRepository.findByUsernameNot(username); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/java/com/lm/utils/Constant.java: -------------------------------------------------------------------------------- 1 | package com.lm.utils; 2 | 3 | /** 4 | * 常量 5 | * @author liangming.deng 6 | * @date 2017年11月20日 7 | * 8 | */ 9 | public class Constant { 10 | public static final String SUCCESS_CODE = "200"; 11 | public static final String SUCCESS_MSG = "SUCCESS"; 12 | 13 | public static final String FAIL_CODE = "500"; 14 | public static final String FAIL_MSG = "用户名或密码不对,请重新登录"; 15 | } 16 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/java/com/lm/vo/UserVo.java: -------------------------------------------------------------------------------- 1 | package com.lm.vo; 2 | 3 | import java.util.List; 4 | 5 | import com.lm.entity.User; 6 | 7 | /** 8 | * 9 | * @author liangming.deng 10 | * @date 2017年11月20日 11 | * 12 | */ 13 | public class UserVo { 14 | private String code; 15 | private String msg; 16 | private List result; 17 | 18 | 19 | public String getCode() { 20 | return code; 21 | } 22 | 23 | public void setCode(String code) { 24 | this.code = code; 25 | } 26 | 27 | public String getMsg() { 28 | return msg; 29 | } 30 | 31 | public void setMsg(String msg) { 32 | this.msg = msg; 33 | } 34 | 35 | public List getResult() { 36 | return result; 37 | } 38 | 39 | public void setResult(List result) { 40 | this.result = result; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "UserVo{" + 46 | "code='" + code + '\'' + 47 | ", msg='" + msg + '\'' + 48 | ", result=" + result + 49 | '}'; 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8089 2 | # mysql 3 | spring.datasource.url=jdbc:mysql://172.16.3.207/spark_test 4 | spring.datasource.username=root 5 | spring.datasource.password=winit2015 6 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 7 | # Number of ms to wait before throwing an exception if no connection is available. 8 | spring.datasource.tomcat.max-wait=10000 9 | # Maximum number of active connections that can be allocated from this pool at the same time. 10 | spring.datasource.tomcat.max-active=50 11 | # Validate the connection before borrowing it from the pool. 12 | spring.datasource.tomcat.test-on-borrow=true 13 | 14 | spring.jpa.show-sql=true -------------------------------------------------------------------------------- /springboot-integ-jpa/src/main/resources/db.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS user; 2 | CREATE TABLE t_user ( 3 | id int(11) NOT NULL AUTO_INCREMENT, 4 | username varchar(32) DEFAULT NULL COMMENT '用户名', 5 | password varchar(32) DEFAULT NULL COMMENT '密码', 6 | create_user varchar(32) DEFAULT NULL COMMENT '创建者', 7 | create_time timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', 8 | update_user varchar(32) DEFAULT NULL COMMENT '更新者', 9 | update_time timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', 10 | PRIMARY KEY (`id`) 11 | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 12 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/test/java/com/lm/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 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 AppTest { 11 | 12 | @Test 13 | public void contextLoads() { 14 | System.out.println("hello world"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot-integ-jpa/src/test/java/com/lm/UserTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | 9 | import com.lm.entity.User; 10 | import com.lm.service.UserService; 11 | 12 | public class UserTest extends AppTest { 13 | 14 | @Autowired 15 | private UserService userService; 16 | 17 | @Test 18 | public void selectPost(){ 19 | List users = userService.selectPost("11", "11"); 20 | 21 | Assert.assertNotNull(users); 22 | } 23 | 24 | @Test 25 | public void selectPut(){ 26 | User user = userService.selectPut(3, "22", "22"); 27 | 28 | Assert.assertNotNull(user); 29 | } 30 | 31 | @Test 32 | public void selectUsernameNot(){ 33 | List users = userService.selectUserNot("11"); 34 | 35 | Assert.assertNotNull(users); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.lm 8 | spring-boot-integration 9 | 0.0.1-SNAPSHOT 10 | 11 | com.lm 12 | springboot-integ-mongo-mdsource 13 | 0.0.1-SNAPSHOT 14 | springboot-integ-mongo-mdsource 15 | http://maven.apache.org 16 | 17 | UTF-8 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-test 27 | test 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-data-mongodb 32 | 33 | 34 | 35 | org.projectlombok 36 | lombok 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-autoconfigure 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | true 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/first/App.java: -------------------------------------------------------------------------------- 1 | package com.lm.first; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | /** 8 | * Hello world! 9 | * 10 | */ 11 | @SpringBootApplication 12 | public class App { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(App.class, args); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/first/config/AbstractMongoConfig.java: -------------------------------------------------------------------------------- 1 | package com.lm.first.config; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.data.mongodb.MongoDbFactory; 7 | import org.springframework.data.mongodb.core.MongoTemplate; 8 | import org.springframework.data.mongodb.core.SimpleMongoDbFactory; 9 | 10 | import com.mongodb.MongoClient; 11 | import com.mongodb.MongoCredential; 12 | import com.mongodb.ServerAddress; 13 | 14 | /** 15 | * 根据配置文件创建MongoDbFactory 16 | * @date 2017年10月14日 17 | * 18 | */ 19 | public abstract class AbstractMongoConfig { 20 | // Mongo DB Properties 21 | private String host, database, username, password; 22 | private int port; 23 | // Setter methods go here.. 24 | 25 | public String getHost() { 26 | return host; 27 | } 28 | 29 | public void setHost(String host) { 30 | this.host = host; 31 | } 32 | 33 | public String getDatabase() { 34 | return database; 35 | } 36 | 37 | public void setDatabase(String database) { 38 | this.database = database; 39 | } 40 | 41 | public String getUsername() { 42 | return username; 43 | } 44 | 45 | public void setUsername(String username) { 46 | this.username = username; 47 | } 48 | 49 | public String getPassword() { 50 | return password; 51 | } 52 | 53 | public void setPassword(String password) { 54 | this.password = password; 55 | } 56 | 57 | public int getPort() { 58 | return port; 59 | } 60 | 61 | public void setPort(int port) { 62 | this.port = port; 63 | } 64 | 65 | /* 66 | * Method that creates MongoDbFactory Common to both of the MongoDb 67 | * connections 68 | */ 69 | public MongoDbFactory mongoDbFactory() throws Exception { 70 | ServerAddress serverAddress = new ServerAddress(host, port); 71 | List mongoCredentialList = new ArrayList<>(); 72 | mongoCredentialList.add(MongoCredential.createCredential(username, database, password.toCharArray())); 73 | return new SimpleMongoDbFactory(new MongoClient(serverAddress, mongoCredentialList), database); 74 | } 75 | 76 | /* 77 | * Factory method to create the MongoTemplate 78 | */ 79 | abstract public MongoTemplate getMongoTemplate() throws Exception; 80 | } 81 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/first/config/PrimaryMongoConfig.java: -------------------------------------------------------------------------------- 1 | package com.lm.first.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.annotation.Primary; 7 | import org.springframework.data.mongodb.core.MongoTemplate; 8 | 9 | @Configuration //Configuration class 10 | @ConfigurationProperties(prefix = "spring.data.mongodb.primary") //Defines my custom prefix and points to the primary db properties 11 | public class PrimaryMongoConfig extends AbstractMongoConfig { 12 | /** 13 | * Implementation of the MongoTemplate factory method 14 | * @Bean gives a name (primaryMongoTemplate) to the created MongoTemplate instance 15 | * @Primary declares that if MongoTemplate is autowired without providing a specific name, 16 | * this is the instance which will be mapped by default 17 | */ 18 | @Primary 19 | @Override 20 | public @Bean(name = "primaryMongoTemplate") MongoTemplate getMongoTemplate() throws Exception { 21 | return new MongoTemplate(mongoDbFactory()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/first/config/SecondaryMongoConfig.java: -------------------------------------------------------------------------------- 1 | package com.lm.first.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.data.mongodb.core.MongoTemplate; 7 | 8 | @Configuration //Configuration 9 | @ConfigurationProperties(prefix = "spring.data.mongodb.secondary") //Defines my custom prefix and points to the secondary db properties 10 | public class SecondaryMongoConfig extends AbstractMongoConfig{ 11 | /** 12 | * Implementation of the MongoTemplate factory method 13 | * @Bean gives a name (primaryMongoTemplate) to the created MongoTemplate instance 14 | * Note that this method doesn't have @Primary 15 | */ 16 | @Override public @Bean(name = "secondaryMongoTemplate") 17 | MongoTemplate getMongoTemplate() throws Exception { 18 | return new MongoTemplate(mongoDbFactory()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/first/dao/MongoObjectDao.java: -------------------------------------------------------------------------------- 1 | package com.lm.first.dao; 2 | 3 | import com.lm.first.entry.PrimaryMongoObject; 4 | import com.lm.first.entry.SecondaryMongoObject; 5 | 6 | public interface MongoObjectDao { 7 | public void savePrimary(PrimaryMongoObject primaryMongoObject); 8 | 9 | public void saveSecondary(SecondaryMongoObject secondaryMongoObject); 10 | 11 | public long getCount(String value); 12 | } 13 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/first/dao/MongoObjectDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.lm.first.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.beans.factory.annotation.Qualifier; 5 | import org.springframework.data.mongodb.core.MongoTemplate; 6 | import org.springframework.data.mongodb.core.query.Criteria; 7 | import org.springframework.data.mongodb.core.query.Query; 8 | import org.springframework.stereotype.Service; 9 | 10 | import com.lm.first.entry.PrimaryMongoObject; 11 | import com.lm.first.entry.SecondaryMongoObject; 12 | 13 | /** 14 | * 业务实现 15 | * @date 2017年10月14日 16 | * 17 | */ 18 | @Service 19 | public class MongoObjectDaoImpl implements MongoObjectDao { 20 | 21 | // Using MongoTemplate for primary database 22 | @Autowired 23 | @Qualifier(value = "primaryMongoTemplate") 24 | protected MongoTemplate primaryMongoTemplate; 25 | 26 | // Using mongoTemplate for secondary database 27 | @Autowired 28 | @Qualifier(value = "secondaryMongoTemplate") 29 | protected MongoTemplate secondaryMongoTemplate; 30 | 31 | @Override 32 | public void savePrimary(PrimaryMongoObject primaryMongoObject) { 33 | primaryMongoTemplate.save(primaryMongoObject); 34 | } 35 | 36 | @Override 37 | public void saveSecondary(SecondaryMongoObject secondaryMongoObject) { 38 | secondaryMongoTemplate.save(secondaryMongoObject); 39 | } 40 | 41 | @Override 42 | public long getCount(String value) { 43 | Query query = new Query(Criteria.where("value").is(value)); 44 | long primary = primaryMongoTemplate.count(query, PrimaryMongoObject.class); 45 | long secondary = secondaryMongoTemplate.count(query, SecondaryMongoObject.class); 46 | return (primary + secondary); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/first/entry/PrimaryMongoObject.java: -------------------------------------------------------------------------------- 1 | package com.lm.first.entry; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | /** 7 | * @author primary对象 8 | */ 9 | @Document(collection = "first_mongo") 10 | public class PrimaryMongoObject { 11 | 12 | @Id 13 | private String id; 14 | 15 | private String value; 16 | 17 | public String getId() { 18 | return id; 19 | } 20 | 21 | 22 | public void setId(String id) { 23 | this.id = id; 24 | } 25 | 26 | 27 | public String getValue() { 28 | return value; 29 | } 30 | 31 | 32 | public void setValue(String value) { 33 | this.value = value; 34 | } 35 | 36 | 37 | @Override 38 | public String toString() { 39 | return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\'' 40 | + '}'; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/first/entry/SecondaryMongoObject.java: -------------------------------------------------------------------------------- 1 | package com.lm.first.entry; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | /** 7 | * @author secondary对象 8 | */ 9 | @Document(collection = "second_mongo") 10 | public class SecondaryMongoObject { 11 | 12 | @Id 13 | private String id; 14 | 15 | private String value; 16 | 17 | 18 | public String getId() { 19 | return id; 20 | } 21 | 22 | 23 | public void setId(String id) { 24 | this.id = id; 25 | } 26 | 27 | 28 | public String getValue() { 29 | return value; 30 | } 31 | 32 | 33 | public void setValue(String value) { 34 | this.value = value; 35 | } 36 | 37 | 38 | @Override 39 | public String toString() { 40 | return "SecondaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\'' 41 | + '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/lombok/entity/Test.java: -------------------------------------------------------------------------------- 1 | package com.lm.lombok.entity; 2 | 3 | import lombok.Data; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.NonNull; 7 | import lombok.Setter; 8 | 9 | @Data 10 | @NoArgsConstructor 11 | @Getter 12 | @Setter 13 | public class Test { 14 | 15 | @NonNull 16 | private String id; 17 | @NonNull 18 | private String msg; 19 | 20 | 21 | public static void main(String[] args){ 22 | Test test = new Test(); 23 | test.getId(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/App.java: -------------------------------------------------------------------------------- 1 | package com.lm.second; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; 6 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 7 | 8 | import com.lm.second.config.props.MultipleMongoProperties; 9 | 10 | 11 | /** 12 | * Hello world! 13 | * 14 | */ 15 | @EnableConfigurationProperties(MultipleMongoProperties.class) 16 | @SpringBootApplication(exclude = MongoAutoConfiguration.class) 17 | public class App { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(App.class, args); 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/config/MultipleMongoConfig.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.config; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Qualifier; 8 | import org.springframework.boot.autoconfigure.mongo.MongoProperties; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.context.annotation.Primary; 12 | import org.springframework.data.mongodb.MongoDbFactory; 13 | import org.springframework.data.mongodb.core.MongoTemplate; 14 | import org.springframework.data.mongodb.core.SimpleMongoDbFactory; 15 | 16 | import com.lm.second.config.props.MultipleMongoProperties; 17 | import com.mongodb.MongoClient; 18 | import com.mongodb.MongoCredential; 19 | import com.mongodb.ServerAddress; 20 | 21 | /** 22 | * 获取各数据源MongoTemplate 23 | * 24 | * @author liangming.deng 25 | * @date 2017年10月14日 26 | * 27 | */ 28 | @Configuration 29 | public class MultipleMongoConfig { 30 | 31 | @Autowired 32 | private MultipleMongoProperties mongoProperties; 33 | 34 | @Primary 35 | @Bean 36 | @Qualifier(PrimaryMongoConfig.MONGO_TEMPLATE) 37 | public MongoTemplate primaryMongoTemplate() throws Exception { 38 | return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary())); 39 | } 40 | 41 | @Bean 42 | @Qualifier(SecondaryMongoConfig.MONGO_TEMPLATE) 43 | public MongoTemplate secondaryMongoTemplate() throws Exception { 44 | return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary())); 45 | } 46 | 47 | @Bean 48 | @Primary 49 | public MongoDbFactory primaryFactory(MongoProperties mongo) throws Exception { 50 | ServerAddress serverAddress = new ServerAddress(mongo.getHost(), mongo.getPort()); 51 | List mongoCredentialList = new ArrayList<>(); 52 | mongoCredentialList 53 | .add(MongoCredential.createCredential(mongo.getUsername(), mongo.getDatabase(), mongo.getPassword())); 54 | return new SimpleMongoDbFactory(new MongoClient(serverAddress, mongoCredentialList), mongo.getDatabase()); 55 | } 56 | 57 | @Bean 58 | public MongoDbFactory secondaryFactory(MongoProperties mongo) throws Exception { 59 | ServerAddress serverAddress = new ServerAddress(mongo.getHost(), mongo.getPort()); 60 | List mongoCredentialList = new ArrayList<>(); 61 | mongoCredentialList 62 | .add(MongoCredential.createCredential(mongo.getUsername(), mongo.getDatabase(), mongo.getPassword())); 63 | return new SimpleMongoDbFactory(new MongoClient(serverAddress, mongoCredentialList), mongo.getDatabase()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/config/PrimaryMongoConfig.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 5 | 6 | /** 7 | * @author 8 | * 主数据源 数据层 9 | */ 10 | @Configuration 11 | @EnableMongoRepositories(basePackages = "com.lm.second.repository.primary", 12 | mongoTemplateRef = PrimaryMongoConfig.MONGO_TEMPLATE) 13 | public class PrimaryMongoConfig { 14 | 15 | protected static final String MONGO_TEMPLATE = "primaryMongoTemplate"; 16 | } 17 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/config/SecondaryMongoConfig.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 5 | 6 | /** 7 | * @author 8 | * 第二数据源 数据层 9 | */ 10 | @Configuration 11 | @EnableMongoRepositories(basePackages = "com.lm.second.repository.secondary", 12 | mongoTemplateRef = SecondaryMongoConfig.MONGO_TEMPLATE) 13 | public class SecondaryMongoConfig { 14 | 15 | protected static final String MONGO_TEMPLATE = "secondaryMongoTemplate"; 16 | } 17 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/config/props/MultipleMongoProperties.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.config.props; 2 | 3 | import org.springframework.boot.autoconfigure.mongo.MongoProperties; 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | 6 | import lombok.Data; 7 | /** 8 | * 读取配置文件 9 | * @author liangming.deng 10 | * @date 2017年10月14日 11 | * 12 | */ 13 | 14 | @Data 15 | @ConfigurationProperties(prefix = "spring.data.mongodb") 16 | public class MultipleMongoProperties { 17 | 18 | private MongoProperties primary = new MongoProperties(); 19 | private MongoProperties secondary = new MongoProperties(); 20 | } 21 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/dao/SecondMongoObjectDao.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.dao; 2 | 3 | import com.lm.second.entity.PrimaryMongoObject; 4 | import com.lm.second.entity.SecondaryMongoObject; 5 | 6 | public interface SecondMongoObjectDao { 7 | public void savePrimary(PrimaryMongoObject primaryMongoObject); 8 | 9 | public void saveSecondary(SecondaryMongoObject secondaryMongoObject); 10 | 11 | public long getCount(String value); 12 | } 13 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/dao/SecondMongoObjectDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.dao; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.lm.second.entity.PrimaryMongoObject; 7 | import com.lm.second.entity.SecondaryMongoObject; 8 | import com.lm.second.repository.primary.PrimaryRepository; 9 | import com.lm.second.repository.secondary.SecondaryRepository; 10 | 11 | /** 12 | * 业务实现 13 | * 14 | * @date 2017年10月14日 15 | * 16 | */ 17 | @Service 18 | public class SecondMongoObjectDaoImpl implements SecondMongoObjectDao { 19 | 20 | @Autowired 21 | private PrimaryRepository primaryRepository; 22 | @Autowired 23 | private SecondaryRepository secondaryRepository; 24 | 25 | @Override 26 | public void savePrimary(PrimaryMongoObject primaryMongoObject) { 27 | primaryRepository.save(primaryMongoObject); 28 | } 29 | 30 | @Override 31 | public void saveSecondary(SecondaryMongoObject secondaryMongoObject) { 32 | secondaryRepository.save(secondaryMongoObject); 33 | } 34 | 35 | @Override 36 | public long getCount(String value) { 37 | 38 | long primary = primaryRepository.findAll().size(); 39 | long secondary = secondaryRepository.findAll().size(); 40 | return (primary + secondary); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/entity/PrimaryMongoObject.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import org.springframework.data.annotation.Id; 7 | import org.springframework.data.mongodb.core.mapping.Document; 8 | 9 | /** 10 | * @author 11 | */ 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | @Document(collection = "first_mongo") 16 | public class PrimaryMongoObject { 17 | 18 | @Id 19 | private String id; 20 | 21 | private String value; 22 | 23 | @Override 24 | public String toString() { 25 | return "PrimaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\'' 26 | + '}'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/entity/SecondaryMongoObject.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.entity; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import org.springframework.data.annotation.Id; 7 | import org.springframework.data.mongodb.core.mapping.Document; 8 | 9 | /** 10 | * 11 | * @author liangming.deng 12 | * @date 2017年10月14日 13 | * 14 | */ 15 | @Data 16 | @AllArgsConstructor 17 | @NoArgsConstructor 18 | @Document(collection = "second_mongo") 19 | public class SecondaryMongoObject { 20 | 21 | @Id 22 | private String id; 23 | 24 | private String value; 25 | 26 | public SecondaryMongoObject(String id){ 27 | this.id = id; 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return "SecondaryMongoObject{" + "id='" + id + '\'' + ", value='" + value + '\'' 33 | + '}'; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/repository/primary/PrimaryRepository.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.repository.primary; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | 5 | import com.lm.second.entity.PrimaryMongoObject; 6 | 7 | public interface PrimaryRepository extends MongoRepository { 8 | } 9 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/java/com/lm/second/repository/secondary/SecondaryRepository.java: -------------------------------------------------------------------------------- 1 | package com.lm.second.repository.secondary; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | 5 | import com.lm.second.entity.SecondaryMongoObject; 6 | 7 | public interface SecondaryRepository extends MongoRepository { 8 | } 9 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=spirngboot-integ-mongo-mdsource 2 | 3 | spring.data.mongodb.primary.database=logs 4 | spring.data.mongodb.primary.host=172.16.3.68 5 | spring.data.mongodb.primary.password=user1 6 | spring.data.mongodb.primary.port=27017 7 | spring.data.mongodb.primary.username=user1 8 | 9 | spring.data.mongodb.secondary.database=t_user 10 | spring.data.mongodb.secondary.host=172.16.3.68 11 | spring.data.mongodb.secondary.password=user1 12 | spring.data.mongodb.secondary.port=27017 13 | spring.data.mongodb.secondary.username=user1 14 | 15 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/test/java/com/lm/first/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.lm.first; 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 AppTest { 11 | 12 | @Test 13 | public void testApp() { 14 | System.out.println("hello world"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/test/java/com/lm/first/MongoDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.lm.first; 2 | 3 | import java.util.Date; 4 | 5 | import org.junit.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | 8 | import com.lm.first.dao.MongoObjectDao; 9 | import com.lm.first.entry.PrimaryMongoObject; 10 | import com.lm.first.entry.SecondaryMongoObject; 11 | 12 | public class MongoDaoTest extends AppTest { 13 | 14 | @Autowired 15 | private MongoObjectDao mongoObjectDao; 16 | 17 | @Test 18 | public void testSavePrimary() throws Exception { 19 | PrimaryMongoObject primaryMongoObject = new PrimaryMongoObject(); 20 | primaryMongoObject.setId("p1" + new Date().getTime()); 21 | primaryMongoObject.setValue("xiaoming"); 22 | mongoObjectDao.savePrimary(primaryMongoObject); 23 | } 24 | 25 | @Test 26 | public void testSaveSecondary() { 27 | SecondaryMongoObject secondaryMongoObject = new SecondaryMongoObject(); 28 | secondaryMongoObject.setId("s1" + new Date().getTime()); 29 | secondaryMongoObject.setValue("xiaoming"); 30 | mongoObjectDao.saveSecondary(secondaryMongoObject); 31 | } 32 | 33 | @Test 34 | public void testGetCount() { 35 | 36 | long count = mongoObjectDao.getCount("xiaoming"); 37 | System.out.println("===============================count:" + count); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /springboot-integ-mongo-mdsource/src/test/java/com/lm/second/SecondMongoDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.lm.second; 2 | 3 | import java.util.Date; 4 | 5 | import org.junit.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | 8 | import com.lm.first.AppTest; 9 | import com.lm.second.dao.SecondMongoObjectDao; 10 | import com.lm.second.entity.PrimaryMongoObject; 11 | import com.lm.second.entity.SecondaryMongoObject; 12 | 13 | public class SecondMongoDaoTest extends AppTest { 14 | 15 | @Autowired 16 | private SecondMongoObjectDao secondMongoObjectDao; 17 | 18 | @Test 19 | public void testSavePrimary() throws Exception { 20 | PrimaryMongoObject primaryMongoObject = new PrimaryMongoObject(); 21 | primaryMongoObject.setId("p1" + new Date().getTime()); 22 | primaryMongoObject.setValue("xiaoming1"); 23 | secondMongoObjectDao.savePrimary(primaryMongoObject); 24 | } 25 | 26 | @Test 27 | public void testSaveSecondary() { 28 | SecondaryMongoObject secondaryMongoObject = new SecondaryMongoObject(); 29 | secondaryMongoObject.setId("s1" + new Date().getTime()); 30 | secondaryMongoObject.setValue("xiaoming1"); 31 | secondMongoObjectDao.saveSecondary(secondaryMongoObject); 32 | } 33 | 34 | @Test 35 | public void testGetCount() { 36 | 37 | long count = secondMongoObjectDao.getCount("xiaoming"); 38 | System.out.println("===============================count:" + count); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /springboot-integ-mongo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.lm 7 | spring-boot-integration 8 | 0.0.1-SNAPSHOT 9 | 10 | springboot-integ-mongo 11 | springboot-integ-mongo 12 | http://maven.apache.org 13 | 14 | UTF-8 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-test 24 | test 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-data-mongodb 29 | 30 | 31 | 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-maven-plugin 37 | 38 | true 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /springboot-integ-mongo/src/main/java/com/lm/App.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | /** 8 | * Hello world! 9 | * 10 | */ 11 | @SpringBootApplication 12 | public class App { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(App.class, args); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /springboot-integ-mongo/src/main/java/com/lm/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.lm.dao; 2 | 3 | import com.lm.entity.UserEntity; 4 | 5 | /** 6 | * Created by summer on 2017/5/5. 7 | */ 8 | public interface UserDao { 9 | 10 | public void saveUser(UserEntity user); 11 | 12 | public UserEntity findUserByUserName(String userName); 13 | 14 | public int updateUser(UserEntity user); 15 | 16 | public void deleteUserById(Long id); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /springboot-integ-mongo/src/main/java/com/lm/dao/impl/UserDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.lm.dao.impl; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.data.mongodb.core.MongoTemplate; 5 | import org.springframework.data.mongodb.core.query.Criteria; 6 | import org.springframework.data.mongodb.core.query.Query; 7 | import org.springframework.data.mongodb.core.query.Update; 8 | import org.springframework.stereotype.Component; 9 | 10 | import com.lm.dao.UserDao; 11 | import com.lm.entity.UserEntity; 12 | import com.mongodb.WriteResult; 13 | 14 | @Component 15 | public class UserDaoImpl implements UserDao { 16 | 17 | @Autowired 18 | private MongoTemplate mongoTemplate; 19 | 20 | /** 21 | * 创建对象 22 | * @param user 23 | */ 24 | @Override 25 | public void saveUser(UserEntity user) { 26 | mongoTemplate.save(user); 27 | } 28 | 29 | /** 30 | * 根据用户名查询对象 31 | * @param userName 32 | * @return 33 | */ 34 | @Override 35 | public UserEntity findUserByUserName(String userName) { 36 | Query query=new Query(Criteria.where("userName").is(userName)); 37 | UserEntity user = mongoTemplate.findOne(query , UserEntity.class); 38 | return user; 39 | } 40 | 41 | /** 42 | * 更新对象 43 | * @param user 44 | */ 45 | @Override 46 | public int updateUser(UserEntity user) { 47 | Query query=new Query(Criteria.where("id").is(user.getId())); 48 | Update update= new Update().set("userName", user.getUserName()).set("passWord", user.getPassWord()); 49 | //更新查询返回结果集的第一条 50 | WriteResult result =mongoTemplate.updateFirst(query,update,UserEntity.class); 51 | //更新查询返回结果集的所有 52 | // mongoTemplate.updateMulti(query,update,UserEntity.class); 53 | if(result!=null) 54 | return result.getN(); 55 | else 56 | return 0; 57 | } 58 | 59 | /** 60 | * 删除对象 61 | * @param id 62 | */ 63 | @Override 64 | public void deleteUserById(Long id) { 65 | Query query=new Query(Criteria.where("id").is(id)); 66 | mongoTemplate.remove(query,UserEntity.class); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /springboot-integ-mongo/src/main/java/com/lm/entity/UserEntity.java: -------------------------------------------------------------------------------- 1 | package com.lm.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | public class UserEntity implements Serializable { 6 | private static final long serialVersionUID = -3258839839160856613L; 7 | private Long id; 8 | private String userName; 9 | private String passWord; 10 | 11 | public Long getId() { 12 | return id; 13 | } 14 | 15 | public void setId(Long id) { 16 | this.id = id; 17 | } 18 | 19 | public String getUserName() { 20 | return userName; 21 | } 22 | 23 | public void setUserName(String userName) { 24 | this.userName = userName; 25 | } 26 | 27 | public String getPassWord() { 28 | return passWord; 29 | } 30 | 31 | public void setPassWord(String passWord) { 32 | this.passWord = passWord; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "UserEntity{" + 38 | "id=" + id + 39 | ", userName='" + userName + '\'' + 40 | ", passWord='" + passWord + '\'' + 41 | '}'; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /springboot-integ-mongo/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.application.name=spirngboot-int-mongodbeg 2 | 3 | spring.data.mongodb.database=# 4 | spring.data.mongodb.host=# 5 | spring.data.mongodb.password=# 6 | spring.data.mongodb.port=27017 7 | spring.data.mongodb.username=# 8 | 9 | 10 | -------------------------------------------------------------------------------- /springboot-integ-mongo/src/test/java/com/lm/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 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 AppTest { 11 | 12 | @Test 13 | public void testApp() { 14 | System.out.println("hello world"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot-integ-mongo/src/test/java/com/lm/UserDaoTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import org.junit.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | 6 | import com.lm.dao.UserDao; 7 | import com.lm.entity.UserEntity; 8 | 9 | public class UserDaoTest extends AppTest { 10 | 11 | @Autowired 12 | private UserDao userDao; 13 | 14 | @Test 15 | public void testSaveUser() throws Exception { 16 | UserEntity user=new UserEntity(); 17 | user.setId(2l); 18 | user.setUserName("小明"); 19 | user.setPassWord("xiaoming1"); 20 | userDao.saveUser(user); 21 | } 22 | 23 | @Test 24 | public void findUserByUserName(){ 25 | UserEntity user= userDao.findUserByUserName("小明"); 26 | System.out.println("user is "+user); 27 | } 28 | 29 | @Test 30 | public void updateUser(){ 31 | UserEntity user=new UserEntity(); 32 | user.setId(2l); 33 | user.setUserName("小红"); 34 | user.setPassWord("小红1"); 35 | userDao.updateUser(user); 36 | } 37 | 38 | @Test 39 | public void deleteUserById(){ 40 | userDao.deleteUserById(1l); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.lm 7 | spring-boot-integration 8 | 0.0.1-SNAPSHOT 9 | 10 | springboot-integ-mybatis-annotation-mdsource 11 | springboot-integ-mybatis-annotation-mdsource 12 | http://maven.apache.org 13 | 14 | UTF-8 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-test 24 | test 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.mybatis.spring.boot 32 | mybatis-spring-boot-starter 33 | 1.1.1 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | true 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/main/java/com/lm/App.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | /** 7 | * 应用启动 8 | * 9 | * @author liangming.deng 10 | * @date 2017年6月20日 11 | * 12 | */ 13 | 14 | @SpringBootApplication 15 | public class App { 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(App.class, args); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/main/java/com/lm/datasource/DataSource1Config.java: -------------------------------------------------------------------------------- 1 | package com.lm.datasource; 2 | 3 | import org.apache.ibatis.session.SqlSessionFactory; 4 | import org.mybatis.spring.SqlSessionFactoryBean; 5 | import org.mybatis.spring.SqlSessionTemplate; 6 | import org.mybatis.spring.annotation.MapperScan; 7 | import org.springframework.beans.factory.annotation.Qualifier; 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 9 | import org.springframework.boot.context.properties.ConfigurationProperties; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.context.annotation.Primary; 13 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 14 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 15 | 16 | import javax.sql.DataSource; 17 | 18 | /** 19 | * test1数据源 20 | * 21 | * @author liangming.deng 22 | * @date 2017年6月30日 23 | * 24 | */ 25 | @Configuration 26 | @MapperScan(basePackages = "com.lm.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate") 27 | public class DataSource1Config { 28 | 29 | @Bean(name = "test1DataSource") 30 | @ConfigurationProperties(prefix = "spring.datasource.test1") 31 | @Primary 32 | public DataSource testDataSource() { 33 | return DataSourceBuilder.create().build(); 34 | } 35 | 36 | @Bean(name = "test1SqlSessionFactory") 37 | @Primary 38 | public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) 39 | throws Exception { 40 | SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 41 | bean.setDataSource(dataSource); 42 | return bean.getObject(); 43 | } 44 | 45 | @Bean(name = "test1TransactionManager") 46 | @Primary 47 | public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) { 48 | return new DataSourceTransactionManager(dataSource); 49 | } 50 | 51 | @Bean(name = "test1SqlSessionTemplate") 52 | @Primary 53 | public SqlSessionTemplate testSqlSessionTemplate( 54 | @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 55 | return new SqlSessionTemplate(sqlSessionFactory); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/main/java/com/lm/datasource/DataSource2Config.java: -------------------------------------------------------------------------------- 1 | package com.lm.datasource; 2 | 3 | import org.apache.ibatis.session.SqlSessionFactory; 4 | import org.mybatis.spring.SqlSessionFactoryBean; 5 | import org.mybatis.spring.SqlSessionTemplate; 6 | import org.mybatis.spring.annotation.MapperScan; 7 | import org.springframework.beans.factory.annotation.Qualifier; 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 9 | import org.springframework.boot.context.properties.ConfigurationProperties; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 13 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 14 | 15 | import javax.sql.DataSource; 16 | 17 | /** 18 | * test2数据源 19 | * 20 | * @author liangming.deng 21 | * @date 2017年6月30日 22 | * 23 | */ 24 | @Configuration 25 | @MapperScan(basePackages = "com.lm.mapper.test2", sqlSessionTemplateRef = "test2SqlSessionTemplate") 26 | public class DataSource2Config { 27 | 28 | @Bean(name = "test2DataSource") 29 | @ConfigurationProperties(prefix = "spring.datasource.test2") 30 | public DataSource testDataSource() { 31 | return DataSourceBuilder.create().build(); 32 | } 33 | 34 | @Bean(name = "test2SqlSessionFactory") 35 | public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) 36 | throws Exception { 37 | SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 38 | bean.setDataSource(dataSource); 39 | return bean.getObject(); 40 | } 41 | 42 | @Bean(name = "test2TransactionManager") 43 | public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) { 44 | return new DataSourceTransactionManager(dataSource); 45 | } 46 | 47 | @Bean(name = "test2SqlSessionTemplate") 48 | public SqlSessionTemplate testSqlSessionTemplate( 49 | @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 50 | return new SqlSessionTemplate(sqlSessionFactory); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/main/java/com/lm/entity/UserEntity.java: -------------------------------------------------------------------------------- 1 | package com.lm.entity; 2 | 3 | import java.util.Date; 4 | 5 | import com.lm.enums.SexEnums; 6 | 7 | /** 8 | * 用户信息 9 | * @author liangming.deng 10 | * @date 2017年6月20日 11 | * 12 | */ 13 | public class UserEntity { 14 | /** 15 | * 逐渐 16 | */ 17 | private Long id; 18 | /** 19 | * 用户名 20 | */ 21 | private String userName; 22 | /** 23 | * 昵称 24 | */ 25 | private String nickName; 26 | /** 27 | * 密码 28 | */ 29 | private String passWord; 30 | /** 31 | * 注册时间 32 | */ 33 | private Date regTime; 34 | /** 35 | * 邮箱 36 | */ 37 | private String email; 38 | /** 39 | * 性别 40 | */ 41 | private SexEnums sexEnums; 42 | 43 | public UserEntity(){} 44 | 45 | public UserEntity(String userName, String nickName, String passWord, Date regTime, String email, 46 | SexEnums sexEnums) { 47 | super(); 48 | this.userName = userName; 49 | this.nickName = nickName; 50 | this.passWord = passWord; 51 | this.regTime = regTime; 52 | this.email = email; 53 | this.sexEnums = sexEnums; 54 | } 55 | 56 | public Long getId() { 57 | return id; 58 | } 59 | public void setId(Long id) { 60 | this.id = id; 61 | } 62 | public String getUserName() { 63 | return userName; 64 | } 65 | public void setUserName(String userName) { 66 | this.userName = userName; 67 | } 68 | public String getNickName() { 69 | return nickName; 70 | } 71 | public void setNickName(String nickName) { 72 | this.nickName = nickName; 73 | } 74 | public String getPassWord() { 75 | return passWord; 76 | } 77 | public void setPassWord(String passWord) { 78 | this.passWord = passWord; 79 | } 80 | public Date getRegTime() { 81 | return regTime; 82 | } 83 | public void setRegTime(Date regTime) { 84 | this.regTime = regTime; 85 | } 86 | public String getEmail() { 87 | return email; 88 | } 89 | public void setEmail(String email) { 90 | this.email = email; 91 | } 92 | public SexEnums getSexEnums() { 93 | return sexEnums; 94 | } 95 | public void setSexEnums(SexEnums sexEnums) { 96 | this.sexEnums = sexEnums; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/main/java/com/lm/enums/SexEnums.java: -------------------------------------------------------------------------------- 1 | package com.lm.enums; 2 | 3 | /** 4 | * 性别 5 | * @author liangming.deng 6 | * @date 2017年6月20日 7 | * 8 | */ 9 | public enum SexEnums { 10 | MAN, WOMAN 11 | } 12 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/main/java/com/lm/mapper/test1/User1Mapper.java: -------------------------------------------------------------------------------- 1 | package com.lm.mapper.test1; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Delete; 6 | import org.apache.ibatis.annotations.Insert; 7 | import org.apache.ibatis.annotations.Result; 8 | import org.apache.ibatis.annotations.Results; 9 | import org.apache.ibatis.annotations.Select; 10 | import org.apache.ibatis.annotations.Update; 11 | 12 | import com.lm.entity.UserEntity; 13 | import com.lm.enums.SexEnums; 14 | /** 15 | * mybatis中mapper接口 16 | * @author liangming.deng 17 | * @date 2017年6月21日 18 | * 19 | */ 20 | public interface User1Mapper { 21 | @Select("select * from user") 22 | @Results({ 23 | @Result(id=true,property="id",column="id"), 24 | @Result(property="userName",column="userName"), 25 | @Result(property="nickName",column="nickName"), 26 | @Result(property="passWord",column="passWord"), 27 | @Result(property="email",column="email"), 28 | @Result(property="regTime",column="regTime"), 29 | @Result(property="sexEnums",column="sex",javaType=SexEnums.class)}) 30 | List getAll(); 31 | 32 | @Select("select * from user where id=#{id}") 33 | @Results({ 34 | @Result(id=true,property="id",column="id"), 35 | @Result(property="userName",column="userName"), 36 | @Result(property="nickName",column="nickName"), 37 | @Result(property="passWord",column="passWord"), 38 | @Result(property="email",column="email"), 39 | @Result(property="regTime",column="regTime"), 40 | @Result(property="sexEnums",column="sex",javaType=SexEnums.class)}) 41 | UserEntity getUserById(Long id); 42 | 43 | @Insert("INSERT INTO user(userName,nickName,passWord,email,regTime,sex) " 44 | + "VALUES(#{userName}, #{nickName}, #{passWord}, #{email}, #{regTime}, #{sex}) ") 45 | void insert(UserEntity user); 46 | 47 | 48 | @Update("UPDATE user SET userName=#{userName},nickName=#{nickName} where id=#{id}") 49 | void update(UserEntity user); 50 | 51 | @Delete("DELETE FROM user where id=#{id}") 52 | void delete(Long id); 53 | } 54 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/main/java/com/lm/mapper/test2/User2Mapper.java: -------------------------------------------------------------------------------- 1 | package com.lm.mapper.test2; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Delete; 6 | import org.apache.ibatis.annotations.Insert; 7 | import org.apache.ibatis.annotations.Result; 8 | import org.apache.ibatis.annotations.Results; 9 | import org.apache.ibatis.annotations.Select; 10 | import org.apache.ibatis.annotations.Update; 11 | 12 | import com.lm.entity.UserEntity; 13 | import com.lm.enums.SexEnums; 14 | /** 15 | * mybatis中mapper接口 16 | * @author liangming.deng 17 | * @date 2017年6月21日 18 | * 19 | */ 20 | public interface User2Mapper { 21 | @Select("select * from user") 22 | @Results({ 23 | @Result(id=true,property="id",column="id"), 24 | @Result(property="userName",column="userName"), 25 | @Result(property="nickName",column="nickName"), 26 | @Result(property="passWord",column="passWord"), 27 | @Result(property="email",column="email"), 28 | @Result(property="regTime",column="regTime"), 29 | @Result(property="sexEnums",column="sex",javaType=SexEnums.class)}) 30 | List getAll(); 31 | 32 | @Select("select * from user where id=#{id}") 33 | @Results({ 34 | @Result(id=true,property="id",column="id"), 35 | @Result(property="userName",column="userName"), 36 | @Result(property="nickName",column="nickName"), 37 | @Result(property="passWord",column="passWord"), 38 | @Result(property="email",column="email"), 39 | @Result(property="regTime",column="regTime"), 40 | @Result(property="sexEnums",column="sex",javaType=SexEnums.class)}) 41 | UserEntity getUserById(Long id); 42 | 43 | @Insert("INSERT INTO user(userName,nickName,passWord,email,regTime,sex) " 44 | + "VALUES(#{userName}, #{nickName}, #{passWord}, #{email}, #{regTime}, #{sex}) ") 45 | void insert(UserEntity user); 46 | 47 | 48 | @Update("UPDATE user SET userName=#{userName},nickName=#{nickName} where id=#{id}") 49 | void update(UserEntity user); 50 | 51 | @Delete("DELETE FROM user where id=#{id}") 52 | void delete(Long id); 53 | } 54 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/main/java/com/lm/web/UserController.java: -------------------------------------------------------------------------------- 1 | package com.lm.web; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import com.lm.entity.UserEntity; 11 | import com.lm.mapper.test1.User1Mapper; 12 | import com.lm.mapper.test2.User2Mapper; 13 | /** 14 | * 前端控制器 15 | * @author liangming.deng 16 | * @date 2017年6月21日 17 | * 18 | */ 19 | @RestController 20 | public class UserController { 21 | 22 | @Autowired 23 | private User2Mapper user2Mapper; 24 | 25 | @Autowired 26 | private User1Mapper user1Mapper; 27 | @RequestMapping("/getUsers") 28 | public List getUsers() { 29 | List users = user2Mapper.getAll(); 30 | return users; 31 | } 32 | 33 | @RequestMapping("/getAllUserCount") 34 | public String getAllUserCount(){ 35 | int user1Count = user1Mapper.getAll().size(); 36 | int user2Count = user2Mapper.getAll().size(); 37 | 38 | return "user1Count:" + user1Count + " add " + "user2Count:" + user2Count + " = " + (user1Count+user2Count); 39 | } 40 | 41 | @RequestMapping("/getUser") 42 | public UserEntity getUser(Long id) { 43 | UserEntity user = user2Mapper.getUserById(id); 44 | return user; 45 | } 46 | 47 | @RequestMapping("/add") 48 | public void save(UserEntity user) { 49 | user2Mapper.insert(user); 50 | } 51 | 52 | @RequestMapping(value = "update") 53 | public void update(UserEntity user) { 54 | user2Mapper.update(user); 55 | } 56 | 57 | @RequestMapping(value = "/delete/{id}") 58 | public void delete(@PathVariable("id") Long id) { 59 | user2Mapper.delete(id); 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver 2 | spring.datasource.test1.url = jdbc:mysql://192.168.32.128:3306/test?useUnicode=true&characterEncoding=utf-8 3 | spring.datasource.test1.username = root 4 | spring.datasource.test1.password = root 5 | 6 | 7 | spring.datasource.test2.driverClassName = com.mysql.jdbc.Driver 8 | spring.datasource.test2.url = jdbc:mysql://192.168.32.128:3306/test2?useUnicode=true&characterEncoding=utf-8 9 | spring.datasource.test2.username = root 10 | spring.datasource.test2.password = root 11 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation-mdsource/src/test/java/com/lm/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 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 AppTest { 11 | 12 | @Test 13 | public void contextLoads() { 14 | System.out.println("hello world"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.lm 7 | spring-boot-integration 8 | 0.0.1-SNAPSHOT 9 | 10 | springboot-integ-mybatis-annotation 11 | springboot-integ-mybatis-annotation 12 | http://maven.apache.org 13 | 14 | UTF-8 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-test 24 | test 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.mybatis.spring.boot 32 | mybatis-spring-boot-starter 33 | 1.1.1 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | true 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation/src/main/java/com/lm/App.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | /** 8 | * 应用启动 9 | * 10 | * @author liangming.deng 11 | * @date 2017年6月20日 12 | * 13 | */ 14 | 15 | @SpringBootApplication 16 | @MapperScan("com.lm.mapper") 17 | public class App { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(App.class, args); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation/src/main/java/com/lm/entity/UserEntity.java: -------------------------------------------------------------------------------- 1 | package com.lm.entity; 2 | 3 | import java.util.Date; 4 | 5 | import com.lm.enums.SexEnums; 6 | 7 | /** 8 | * 用户信息 9 | * @author liangming.deng 10 | * @date 2017年6月20日 11 | * 12 | */ 13 | public class UserEntity { 14 | /** 15 | * 逐渐 16 | */ 17 | private Long id; 18 | /** 19 | * 用户名 20 | */ 21 | private String userName; 22 | /** 23 | * 昵称 24 | */ 25 | private String nickName; 26 | /** 27 | * 密码 28 | */ 29 | private String passWord; 30 | /** 31 | * 注册时间 32 | */ 33 | private Date regTime; 34 | /** 35 | * 邮箱 36 | */ 37 | private String email; 38 | /** 39 | * 性别 40 | */ 41 | private SexEnums sexEnums; 42 | 43 | public UserEntity(){} 44 | 45 | public UserEntity(String userName, String nickName, String passWord, Date regTime, String email, 46 | SexEnums sexEnums) { 47 | super(); 48 | this.userName = userName; 49 | this.nickName = nickName; 50 | this.passWord = passWord; 51 | this.regTime = regTime; 52 | this.email = email; 53 | this.sexEnums = sexEnums; 54 | } 55 | 56 | public Long getId() { 57 | return id; 58 | } 59 | public void setId(Long id) { 60 | this.id = id; 61 | } 62 | public String getUserName() { 63 | return userName; 64 | } 65 | public void setUserName(String userName) { 66 | this.userName = userName; 67 | } 68 | public String getNickName() { 69 | return nickName; 70 | } 71 | public void setNickName(String nickName) { 72 | this.nickName = nickName; 73 | } 74 | public String getPassWord() { 75 | return passWord; 76 | } 77 | public void setPassWord(String passWord) { 78 | this.passWord = passWord; 79 | } 80 | public Date getRegTime() { 81 | return regTime; 82 | } 83 | public void setRegTime(Date regTime) { 84 | this.regTime = regTime; 85 | } 86 | public String getEmail() { 87 | return email; 88 | } 89 | public void setEmail(String email) { 90 | this.email = email; 91 | } 92 | public SexEnums getSexEnums() { 93 | return sexEnums; 94 | } 95 | public void setSexEnums(SexEnums sexEnums) { 96 | this.sexEnums = sexEnums; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation/src/main/java/com/lm/enums/SexEnums.java: -------------------------------------------------------------------------------- 1 | package com.lm.enums; 2 | 3 | /** 4 | * 性别 5 | * @author liangming.deng 6 | * @date 2017年6月20日 7 | * 8 | */ 9 | public enum SexEnums { 10 | MAN, WOMAN 11 | } 12 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation/src/main/java/com/lm/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.lm.mapper; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Delete; 6 | import org.apache.ibatis.annotations.Insert; 7 | import org.apache.ibatis.annotations.Result; 8 | import org.apache.ibatis.annotations.Results; 9 | import org.apache.ibatis.annotations.Select; 10 | import org.apache.ibatis.annotations.Update; 11 | 12 | import com.lm.entity.UserEntity; 13 | import com.lm.enums.SexEnums; 14 | /** 15 | * mybatis中mapper接口 16 | * @author liangming.deng 17 | * @date 2017年6月21日 18 | * 19 | */ 20 | public interface UserMapper { 21 | @Select("select * from user") 22 | @Results({ 23 | @Result(id=true,property="id",column="id"), 24 | @Result(property="userName",column="userName"), 25 | @Result(property="nickName",column="nickName"), 26 | @Result(property="passWord",column="passWord"), 27 | @Result(property="email",column="email"), 28 | @Result(property="regTime",column="regTime"), 29 | @Result(property="sexEnums",column="sex",javaType=SexEnums.class)}) 30 | List getAll(); 31 | 32 | @Select("select * from user where id=#{id}") 33 | @Results({ 34 | @Result(id=true,property="id",column="id"), 35 | @Result(property="userName",column="userName"), 36 | @Result(property="nickName",column="nickName"), 37 | @Result(property="passWord",column="passWord"), 38 | @Result(property="email",column="email"), 39 | @Result(property="regTime",column="regTime"), 40 | @Result(property="sexEnums",column="sex",javaType=SexEnums.class)}) 41 | UserEntity getUserById(Long id); 42 | 43 | @Insert("INSERT INTO user(userName,nickName,passWord,email,regTime,sex) " 44 | + "VALUES(#{userName}, #{nickName}, #{passWord}, #{email}, #{regTime}, #{sex}) ") 45 | void insert(UserEntity user); 46 | 47 | 48 | @Update("UPDATE user SET userName=#{userName},nickName=#{nickName} where id=#{id}") 49 | void update(UserEntity user); 50 | 51 | @Delete("DELETE FROM user where id=#{id}") 52 | void delete(Long id); 53 | } 54 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation/src/main/java/com/lm/web/UserController.java: -------------------------------------------------------------------------------- 1 | package com.lm.web; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import com.lm.entity.UserEntity; 11 | import com.lm.mapper.UserMapper; 12 | /** 13 | * 前端控制器 14 | * @author liangming.deng 15 | * @date 2017年6月21日 16 | * 17 | */ 18 | @RestController 19 | public class UserController { 20 | 21 | @Autowired 22 | private UserMapper userMapper; 23 | 24 | @RequestMapping("/getUsers") 25 | public List getUsers() { 26 | List users = userMapper.getAll(); 27 | return users; 28 | } 29 | 30 | @RequestMapping("/getUser") 31 | public UserEntity getUser(Long id) { 32 | UserEntity user = userMapper.getUserById(id); 33 | return user; 34 | } 35 | 36 | @RequestMapping("/add") 37 | public void save(UserEntity user) { 38 | userMapper.insert(user); 39 | } 40 | 41 | @RequestMapping(value = "update") 42 | public void update(UserEntity user) { 43 | userMapper.update(user); 44 | } 45 | 46 | @RequestMapping(value = "/delete/{id}") 47 | public void delete(@PathVariable("id") Long id) { 48 | userMapper.delete(id); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #mybatis.type-aliases-package=com.neo.entity 2 | 3 | spring.datasource.driverClassName = com.mysql.jdbc.Driver 4 | spring.datasource.url = jdbc:mysql://192.168.32.128:3306/test?useUnicode=true&characterEncoding=utf-8 5 | spring.datasource.username = root 6 | spring.datasource.password = root 7 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation/src/test/java/com/lm/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 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 AppTest { 11 | 12 | @Test 13 | public void contextLoads() { 14 | System.out.println("hello world"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-annotation/src/test/java/com/lm/UserMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | 8 | import com.lm.entity.UserEntity; 9 | import com.lm.mapper.UserMapper; 10 | 11 | public class UserMapperTest extends AppTest { 12 | 13 | @Autowired 14 | private UserMapper userMapper; 15 | @Test 16 | public void testGetAlls(){ 17 | List users = userMapper.getAll(); 18 | System.out.println(users.toString()); 19 | } 20 | 21 | @Test 22 | public void testGetUserById(){ 23 | UserEntity user = userMapper.getUserById(15L); 24 | System.out.println(user.toString()); 25 | } 26 | 27 | @Test 28 | public void testDelete(){ 29 | userMapper.delete(16L); 30 | UserEntity user = userMapper.getUserById(16L); 31 | System.out.println(user.toString()); 32 | } 33 | 34 | 35 | @Test 36 | public void testUpdate(){ 37 | UserEntity user = new UserEntity(); 38 | user.setId(15L); 39 | user.setNickName("dlm123"); 40 | user.setUserName("123"); 41 | userMapper.update(user); 42 | UserEntity user1 = userMapper.getUserById(15L); 43 | System.out.println(user1.toString()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.lm 8 | spring-boot-integration 9 | 0.0.1-SNAPSHOT 10 | 11 | springboot-integ-mybatis-xml-mdsource 12 | springboot-integ-mybatis-xml-mdsource 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-test 25 | test 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | 31 | 32 | org.mybatis.spring.boot 33 | mybatis-spring-boot-starter 34 | 1.1.1 35 | 36 | 37 | mysql 38 | mysql-connector-java 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-maven-plugin 47 | 48 | true 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/java/com/lm/App.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(App.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/java/com/lm/datasource/DataSource1Config.java: -------------------------------------------------------------------------------- 1 | package com.lm.datasource; 2 | 3 | import org.apache.ibatis.session.SqlSessionFactory; 4 | import org.mybatis.spring.SqlSessionFactoryBean; 5 | import org.mybatis.spring.SqlSessionTemplate; 6 | import org.mybatis.spring.annotation.MapperScan; 7 | import org.springframework.beans.factory.annotation.Qualifier; 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 9 | import org.springframework.boot.context.properties.ConfigurationProperties; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.context.annotation.Primary; 13 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 14 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 15 | 16 | import javax.sql.DataSource; 17 | 18 | /** 19 | * test1数据源 20 | * 21 | * @author liangming.deng 22 | * @date 2017年6月30日 23 | * 24 | */ 25 | @Configuration 26 | @MapperScan(basePackages = "com.lm.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate") 27 | public class DataSource1Config { 28 | 29 | @Bean(name = "test1DataSource") 30 | @ConfigurationProperties(prefix = "spring.datasource.test1") 31 | @Primary 32 | public DataSource testDataSource() { 33 | return DataSourceBuilder.create().build(); 34 | } 35 | 36 | @Bean(name = "test1SqlSessionFactory") 37 | @Primary 38 | public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) 39 | throws Exception { 40 | SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 41 | bean.setDataSource(dataSource); 42 | bean.setMapperLocations( 43 | new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml")); 44 | return bean.getObject(); 45 | } 46 | 47 | @Bean(name = "test1TransactionManager") 48 | @Primary 49 | public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) { 50 | return new DataSourceTransactionManager(dataSource); 51 | } 52 | 53 | @Bean(name = "test1SqlSessionTemplate") 54 | @Primary 55 | public SqlSessionTemplate testSqlSessionTemplate( 56 | @Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 57 | return new SqlSessionTemplate(sqlSessionFactory); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/java/com/lm/datasource/DataSource2Config.java: -------------------------------------------------------------------------------- 1 | package com.lm.datasource; 2 | 3 | import org.apache.ibatis.session.SqlSessionFactory; 4 | import org.mybatis.spring.SqlSessionFactoryBean; 5 | import org.mybatis.spring.SqlSessionTemplate; 6 | import org.mybatis.spring.annotation.MapperScan; 7 | import org.springframework.beans.factory.annotation.Qualifier; 8 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 9 | import org.springframework.boot.context.properties.ConfigurationProperties; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 13 | import org.springframework.jdbc.datasource.DataSourceTransactionManager; 14 | 15 | import javax.sql.DataSource; 16 | 17 | /** 18 | * test2数据源 19 | * 20 | * @author liangming.deng 21 | * @date 2017年6月30日 22 | * 23 | */ 24 | @Configuration 25 | @MapperScan(basePackages = "com.lm.mapper.test2", sqlSessionTemplateRef = "test2SqlSessionTemplate") 26 | public class DataSource2Config { 27 | 28 | @Bean(name = "test2DataSource") 29 | @ConfigurationProperties(prefix = "spring.datasource.test2") 30 | public DataSource testDataSource() { 31 | return DataSourceBuilder.create().build(); 32 | } 33 | 34 | @Bean(name = "test2SqlSessionFactory") 35 | public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) 36 | throws Exception { 37 | SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 38 | bean.setDataSource(dataSource); 39 | bean.setMapperLocations( 40 | new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml")); 41 | return bean.getObject(); 42 | } 43 | 44 | @Bean(name = "test2TransactionManager") 45 | public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) { 46 | return new DataSourceTransactionManager(dataSource); 47 | } 48 | 49 | @Bean(name = "test2SqlSessionTemplate") 50 | public SqlSessionTemplate testSqlSessionTemplate( 51 | @Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 52 | return new SqlSessionTemplate(sqlSessionFactory); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/java/com/lm/entity/UserEntity.java: -------------------------------------------------------------------------------- 1 | package com.lm.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | 6 | import com.lm.enums.SexEnums; 7 | 8 | 9 | 10 | /** 11 | * 用户信息 12 | * @author liangming.deng 13 | * @date 2017年6月20日 14 | * 15 | */ 16 | public class UserEntity implements Serializable { 17 | /** 18 | * 19 | */ 20 | private static final long serialVersionUID = 1L; 21 | /** 22 | * 主键 23 | */ 24 | private Long id; 25 | /** 26 | * 用户名 27 | */ 28 | private String userName; 29 | /** 30 | * 昵称 31 | */ 32 | private String nickName; 33 | /** 34 | * 密码 35 | */ 36 | private String passWord; 37 | /** 38 | * 注册时间 39 | */ 40 | private Date regTime; 41 | /** 42 | * 邮箱 43 | */ 44 | private String email; 45 | /** 46 | * 性别 47 | */ 48 | private SexEnums sexEnums; 49 | 50 | public UserEntity(){} 51 | 52 | public UserEntity(String userName, String nickName, String passWord, Date regTime, String email, 53 | SexEnums sexEnums) { 54 | super(); 55 | this.userName = userName; 56 | this.nickName = nickName; 57 | this.passWord = passWord; 58 | this.regTime = regTime; 59 | this.email = email; 60 | this.sexEnums = sexEnums; 61 | } 62 | 63 | public Long getId() { 64 | return id; 65 | } 66 | public void setId(Long id) { 67 | this.id = id; 68 | } 69 | public String getUserName() { 70 | return userName; 71 | } 72 | public void setUserName(String userName) { 73 | this.userName = userName; 74 | } 75 | public String getNickName() { 76 | return nickName; 77 | } 78 | public void setNickName(String nickName) { 79 | this.nickName = nickName; 80 | } 81 | public String getPassWord() { 82 | return passWord; 83 | } 84 | public void setPassWord(String passWord) { 85 | this.passWord = passWord; 86 | } 87 | public Date getRegTime() { 88 | return regTime; 89 | } 90 | public void setRegTime(Date regTime) { 91 | this.regTime = regTime; 92 | } 93 | public String getEmail() { 94 | return email; 95 | } 96 | public void setEmail(String email) { 97 | this.email = email; 98 | } 99 | public SexEnums getSexEnums() { 100 | return sexEnums; 101 | } 102 | public void setSexEnums(SexEnums sexEnums) { 103 | this.sexEnums = sexEnums; 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/java/com/lm/enums/SexEnums.java: -------------------------------------------------------------------------------- 1 | package com.lm.enums; 2 | 3 | /** 4 | * 性别 5 | * @author liangming.deng 6 | * @date 2017年6月20日 7 | * 8 | */ 9 | public enum SexEnums { 10 | MAN, WOMAN 11 | } 12 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/java/com/lm/mapper/test1/User1Mapper.java: -------------------------------------------------------------------------------- 1 | package com.lm.mapper.test1; 2 | 3 | import java.util.List; 4 | 5 | import com.lm.entity.UserEntity; 6 | /** 7 | * mybatis中mapper接口 8 | * @author liangming.deng 9 | * @date 2017年6月21日 10 | * 11 | */ 12 | public interface User1Mapper { 13 | List getAll(); 14 | 15 | UserEntity getUserById(Long id); 16 | 17 | void insert(UserEntity user); 18 | 19 | void update(UserEntity user); 20 | 21 | void delete(Long id); 22 | } 23 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/java/com/lm/mapper/test2/User2Mapper.java: -------------------------------------------------------------------------------- 1 | package com.lm.mapper.test2; 2 | 3 | import java.util.List; 4 | 5 | import com.lm.entity.UserEntity; 6 | /** 7 | * mybatis中mapper接口 8 | * @author liangming.deng 9 | * @date 2017年6月21日 10 | * 11 | */ 12 | public interface User2Mapper { 13 | List getAll(); 14 | 15 | UserEntity getUserById(Long id); 16 | 17 | void insert(UserEntity user); 18 | 19 | void update(UserEntity user); 20 | 21 | void delete(Long id); 22 | } 23 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/java/com/lm/web/UserController.java: -------------------------------------------------------------------------------- 1 | package com.lm.web; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import com.lm.entity.UserEntity; 11 | import com.lm.mapper.test1.User1Mapper; 12 | import com.lm.mapper.test2.User2Mapper; 13 | 14 | /** 15 | * 前端控制器 16 | * @author liangming.deng 17 | * @date 2017年6月30日 18 | * 19 | */ 20 | @RestController 21 | public class UserController { 22 | 23 | @Autowired 24 | private User1Mapper user1Mapper; 25 | 26 | @Autowired 27 | private User2Mapper user2Mapper; 28 | 29 | @RequestMapping("/getUsers") 30 | public List getUsers() { 31 | List users=user1Mapper.getAll(); 32 | return users; 33 | } 34 | 35 | @RequestMapping("/getAllUserSize") 36 | public String getAllUserSize() { 37 | int test1UserSize = user1Mapper.getAll().size(); 38 | int test2UserSize = user2Mapper.getAll().size(); 39 | StringBuilder sBuilder = new StringBuilder(); 40 | sBuilder.append("test1UserSize:").append(test1UserSize) 41 | .append( " + ") 42 | .append("test2UserSize:").append(test2UserSize) 43 | .append(" = ").append(test1UserSize+test2UserSize); 44 | return sBuilder.toString(); 45 | } 46 | 47 | @RequestMapping("/getUser") 48 | public UserEntity getUser(Long id) { 49 | UserEntity user=user2Mapper.getUserById(id); 50 | return user; 51 | } 52 | 53 | @RequestMapping("/add") 54 | public void save(UserEntity user) { 55 | user2Mapper.insert(user); 56 | } 57 | 58 | @RequestMapping(value="update") 59 | public void update(UserEntity user) { 60 | user2Mapper.update(user); 61 | } 62 | 63 | @RequestMapping(value="/delete/{id}") 64 | public void delete(@PathVariable("id") Long id) { 65 | user1Mapper.delete(id); 66 | } 67 | 68 | 69 | } -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | mybatis.config-locations=classpath:mybatis/mybatis-config.xml 2 | 3 | spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver 4 | spring.datasource.test1.url = jdbc:mysql://192.168.32.128:3306/test?useUnicode=true&characterEncoding=utf-8 5 | spring.datasource.test1.username = root 6 | spring.datasource.test1.password = root 7 | 8 | 9 | spring.datasource.test2.driverClassName = com.mysql.jdbc.Driver 10 | spring.datasource.test2.url = jdbc:mysql://192.168.32.128:3306/test2?useUnicode=true&characterEncoding=utf-8 11 | spring.datasource.test2.username = root 12 | spring.datasource.test2.password = root 13 | 14 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/resources/mybatis/mapper/test1/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | id, userName, nickName,passWord, email,regTime,sex 16 | 17 | 18 | 23 | 24 | 30 | 31 | 32 | INSERT INTO 33 | user 34 | (userName, nickName,passWord, email,regTime,sex) 35 | VALUES 36 | (#{userName},#{nickName}, #{passWord},#{email}, #{regTime},#{sexEnums}) 37 | 38 | 39 | 40 | UPDATE 41 | user 42 | SET 43 | userName = #{userName}, 44 | passWord = #{passWord} 45 | WHERE 46 | id = #{id} 47 | 48 | 49 | 50 | DELETE FROM 51 | user 52 | WHERE 53 | id =#{id} 54 | 55 | 56 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/resources/mybatis/mapper/test2/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | id, userName, nickName,passWord, email,regTime,sex 16 | 17 | 18 | 23 | 24 | 30 | 31 | 32 | INSERT INTO 33 | user 34 | (userName, nickName,passWord, email,regTime,sex) 35 | VALUES 36 | (#{userName},#{nickName}, #{passWord},#{email}, #{regTime},#{sexEnums}) 37 | 38 | 39 | 40 | UPDATE 41 | user 42 | SET 43 | userName = #{userName}, 44 | passWord = #{passWord} 45 | WHERE 46 | id = #{id} 47 | 48 | 49 | 50 | DELETE FROM 51 | user 52 | WHERE 53 | id =#{id} 54 | 55 | 56 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/resources/mybatis/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/main/resources/user.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `user` ( 2 | `id` bigint(20) NOT NULL AUTO_INCREMENT, 3 | `nickname` varchar(20) DEFAULT NULL COMMENT '用户昵称', 4 | `username` varchar(255) NOT NULL, 5 | `email` varchar(128) DEFAULT NULL COMMENT '邮箱|登录帐号', 6 | `passWord` varchar(32) DEFAULT NULL COMMENT '密码', 7 | `regTime` datetime DEFAULT NULL COMMENT '创建时间', 8 | `sex` varchar(32) DEFAULT NULL, 9 | PRIMARY KEY (`id`) 10 | ) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8; 11 | 12 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/test/java/com/lm/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 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 AppTest { 11 | 12 | @Test 13 | public void contextLoads() { 14 | System.out.println("hello world"); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/test/java/com/lm/User1MapperTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | 8 | import com.lm.entity.UserEntity; 9 | import com.lm.mapper.test1.User1Mapper; 10 | 11 | public class User1MapperTest extends AppTest { 12 | 13 | @Autowired 14 | private User1Mapper userMapper; 15 | 16 | 17 | @Test 18 | public void testQuery() throws Exception { 19 | List users = userMapper.getAll(); 20 | if(users==null || users.size()==0){ 21 | System.out.println("is null"); 22 | }else{ 23 | System.out.println(users.size()); 24 | } 25 | } 26 | 27 | 28 | 29 | } -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml-mdsource/src/test/java/com/lm/User2MapperTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | 8 | import com.lm.entity.UserEntity; 9 | import com.lm.mapper.test2.User2Mapper; 10 | 11 | public class User2MapperTest extends AppTest { 12 | 13 | @Autowired 14 | private User2Mapper userMapper; 15 | 16 | 17 | @Test 18 | public void testQuery() throws Exception { 19 | List users = userMapper.getAll(); 20 | if(users==null || users.size()==0){ 21 | System.out.println("is null"); 22 | }else{ 23 | System.out.println(users.size()); 24 | } 25 | } 26 | 27 | 28 | 29 | } -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lm 6 | spring-boot-integration 7 | 0.0.1-SNAPSHOT 8 | 9 | springboot-integ-mybatis-xml 10 | 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-test 19 | test 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-web 24 | 25 | 26 | org.mybatis.spring.boot 27 | mybatis-spring-boot-starter 28 | 1.1.1 29 | 30 | 31 | mysql 32 | mysql-connector-java 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | true 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/main/java/com/lm/App.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import org.mybatis.spring.annotation.MapperScan; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | /** 8 | * 应用启动 9 | * 10 | * @author liangming.deng 11 | * @date 2017年6月20日 12 | * 13 | */ 14 | 15 | @SpringBootApplication 16 | @MapperScan("com.lm.mapper") 17 | public class App { 18 | 19 | public static void main(String[] args) { 20 | SpringApplication.run(App.class, args); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/main/java/com/lm/entity/UserEntity.java: -------------------------------------------------------------------------------- 1 | package com.lm.entity; 2 | 3 | import java.util.Date; 4 | 5 | import com.lm.enums.SexEnums; 6 | 7 | /** 8 | * 用户信息 9 | * @author liangming.deng 10 | * @date 2017年6月20日 11 | * 12 | */ 13 | public class UserEntity { 14 | /** 15 | * 逐渐 16 | */ 17 | private Long id; 18 | /** 19 | * 用户名 20 | */ 21 | private String userName; 22 | /** 23 | * 昵称 24 | */ 25 | private String nickName; 26 | /** 27 | * 密码 28 | */ 29 | private String passWord; 30 | /** 31 | * 注册时间 32 | */ 33 | private Date regTime; 34 | /** 35 | * 邮箱 36 | */ 37 | private String email; 38 | /** 39 | * 性别 40 | */ 41 | private SexEnums sexEnums; 42 | 43 | public UserEntity(){} 44 | 45 | public UserEntity(String userName, String nickName, String passWord, Date regTime, String email, 46 | SexEnums sexEnums) { 47 | super(); 48 | this.userName = userName; 49 | this.nickName = nickName; 50 | this.passWord = passWord; 51 | this.regTime = regTime; 52 | this.email = email; 53 | this.sexEnums = sexEnums; 54 | } 55 | 56 | public Long getId() { 57 | return id; 58 | } 59 | public void setId(Long id) { 60 | this.id = id; 61 | } 62 | public String getUserName() { 63 | return userName; 64 | } 65 | public void setUserName(String userName) { 66 | this.userName = userName; 67 | } 68 | public String getNickName() { 69 | return nickName; 70 | } 71 | public void setNickName(String nickName) { 72 | this.nickName = nickName; 73 | } 74 | public String getPassWord() { 75 | return passWord; 76 | } 77 | public void setPassWord(String passWord) { 78 | this.passWord = passWord; 79 | } 80 | public Date getRegTime() { 81 | return regTime; 82 | } 83 | public void setRegTime(Date regTime) { 84 | this.regTime = regTime; 85 | } 86 | public String getEmail() { 87 | return email; 88 | } 89 | public void setEmail(String email) { 90 | this.email = email; 91 | } 92 | public SexEnums getSexEnums() { 93 | return sexEnums; 94 | } 95 | public void setSexEnums(SexEnums sexEnums) { 96 | this.sexEnums = sexEnums; 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/main/java/com/lm/enums/SexEnums.java: -------------------------------------------------------------------------------- 1 | package com.lm.enums; 2 | 3 | /** 4 | * 性别 5 | * @author liangming.deng 6 | * @date 2017年6月20日 7 | * 8 | */ 9 | public enum SexEnums { 10 | MAN, WOMAN 11 | } 12 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/main/java/com/lm/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.lm.mapper; 2 | 3 | import java.util.List; 4 | 5 | import com.lm.entity.UserEntity; 6 | /** 7 | * mybatis中mapper接口 8 | * @author liangming.deng 9 | * @date 2017年6月21日 10 | * 11 | */ 12 | public interface UserMapper { 13 | List getAll(); 14 | 15 | UserEntity getUserById(Long id); 16 | 17 | void insert(UserEntity user); 18 | 19 | void update(UserEntity user); 20 | 21 | void delete(Long id); 22 | } 23 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/main/java/com/lm/web/UserController.java: -------------------------------------------------------------------------------- 1 | package com.lm.web; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import com.lm.entity.UserEntity; 11 | import com.lm.mapper.UserMapper; 12 | /** 13 | * 前端控制器 14 | * @author liangming.deng 15 | * @date 2017年6月21日 16 | * 17 | */ 18 | @RestController 19 | public class UserController { 20 | 21 | @Autowired 22 | private UserMapper userMapper; 23 | 24 | @RequestMapping("/getUsers") 25 | public List getUsers() { 26 | List users = userMapper.getAll(); 27 | return users; 28 | } 29 | 30 | @RequestMapping("/getUser") 31 | public UserEntity getUser(Long id) { 32 | UserEntity user = userMapper.getUserById(id); 33 | return user; 34 | } 35 | 36 | @RequestMapping("/add") 37 | public void save(UserEntity user) { 38 | userMapper.insert(user); 39 | } 40 | 41 | @RequestMapping(value = "update") 42 | public void update(UserEntity user) { 43 | userMapper.update(user); 44 | } 45 | 46 | @RequestMapping(value = "/delete/{id}") 47 | public void delete(@PathVariable("id") Long id) { 48 | userMapper.delete(id); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | ###################mybatis配置######################################## 2 | mybatis.config-locations=classpath:mybatis/mybatis-config.xml 3 | mybatis.mapper-locations=classpath:mybatis/mapper/*.xml 4 | mybatis.type-aliases-package=com.lm.entity 5 | 6 | ###################mysql数据源配置############################################### 7 | spring.datasource.driverClassName = com.mysql.jdbc.Driver 8 | spring.datasource.url = jdbc:mysql://192.168.32.128:3306/test?useUnicode=true&characterEncoding=utf-8 9 | spring.datasource.username = root 10 | spring.datasource.password = root 11 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/main/resources/mybatis/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | id, userName, nickName,passWord, email,regTime,sex 16 | 17 | 18 | 23 | 24 | 30 | 31 | 32 | INSERT INTO 33 | user 34 | (userName, nickName,passWord, email,regTime,sex) 35 | VALUES 36 | (#{userName},#{nickName}, #{passWord},#{email}, #{regTime},#{sexEnums}) 37 | 38 | 39 | 40 | UPDATE 41 | user 42 | SET 43 | userName = #{userName}, 44 | passWord = #{passWord} 45 | WHERE 46 | id = #{id} 47 | 48 | 49 | 50 | DELETE FROM 51 | user 52 | WHERE 53 | id =#{id} 54 | 55 | 56 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/main/resources/mybatis/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/main/resources/user.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `user` ( 2 | `id` bigint(20) NOT NULL AUTO_INCREMENT, 3 | `nickname` varchar(20) DEFAULT NULL COMMENT '用户昵称', 4 | `username` varchar(255) NOT NULL, 5 | `email` varchar(128) DEFAULT NULL COMMENT '邮箱|登录帐号', 6 | `passWord` varchar(32) DEFAULT NULL COMMENT '密码', 7 | `regTime` datetime DEFAULT NULL COMMENT '创建时间', 8 | `sex` varchar(32) DEFAULT NULL, 9 | PRIMARY KEY (`id`) 10 | ) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8; 11 | 12 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/test/java/com/lm/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 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 | 9 | /** 10 | * Unit test for simple App. 11 | */ 12 | @RunWith(SpringRunner.class) 13 | @SpringBootTest 14 | public class AppTest { 15 | 16 | @Test 17 | public void testApp() { 18 | System.out.println("hello world"); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /springboot-integ-mybatis-xml/src/test/java/com/lm/UserMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | import java.util.Date; 4 | import java.util.List; 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | 10 | import com.lm.entity.UserEntity; 11 | import com.lm.enums.SexEnums; 12 | import com.lm.mapper.UserMapper; 13 | 14 | 15 | 16 | public class UserMapperTest extends AppTest { 17 | @Autowired 18 | private UserMapper userMapper; 19 | 20 | @Test 21 | public void testInsert() throws Exception { 22 | userMapper.insert(new UserEntity("aa", "aa","a123456",new Date(),"aa@126.com", SexEnums.MAN)); 23 | userMapper.insert(new UserEntity("bb", "bb","a123456",new Date(),"bb@126.com", SexEnums.MAN)); 24 | userMapper.insert(new UserEntity("cc", "cc","a123456",new Date(),"cc@126.com", SexEnums.WOMAN)); 25 | 26 | Assert.assertNotNull(userMapper.getAll().size()); 27 | } 28 | 29 | @Test 30 | public void testQuery() throws Exception { 31 | List users = userMapper.getAll(); 32 | if(users==null || users.size()==0){ 33 | System.out.println("is null"); 34 | }else{ 35 | System.out.println(users.toString()); 36 | } 37 | } 38 | 39 | 40 | @Test 41 | public void testUpdate() throws Exception { 42 | UserEntity user = userMapper.getUserById(15L); 43 | System.out.println(user.toString()); 44 | user.setUserName("dd"); 45 | userMapper.update(user); 46 | Assert.assertTrue(("dd".equals(userMapper.getUserById(15L).getUserName()))); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /springboot-integ-redis/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 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 | -------------------------------------------------------------------------------- /springboot-integ-redis/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /bin/ 3 | /settings/ 4 | /.classpath 5 | /.settings/ 6 | /.project 7 | /.gitignore -------------------------------------------------------------------------------- /springboot-integ-redis/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | springboot-integ-redis 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /springboot-integ-redis/.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java=UTF-8 3 | encoding//src/main/resources=UTF-8 4 | encoding//src/test/java=UTF-8 5 | encoding//src/test/resources=UTF-8 6 | encoding/=UTF-8 7 | -------------------------------------------------------------------------------- /springboot-integ-redis/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 3 | org.eclipse.jdt.core.compiler.compliance=1.8 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.8 6 | -------------------------------------------------------------------------------- /springboot-integ-redis/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /springboot-integ-redis/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.lm 6 | spring-boot-integration 7 | 0.0.1-SNAPSHOT 8 | 9 | springboot-integ-redis 10 | 11 | UTF-8 12 | 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-test 21 | test 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.mybatis.spring.boot 29 | mybatis-spring-boot-starter 30 | 1.1.1 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-data-redis 35 | 36 | 37 | 38 | mysql 39 | mysql-connector-java 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-data-jpa 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-jdbc 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-starter-aop 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-starter-thymeleaf 57 | 58 | 59 | 60 | com.alibaba 61 | druid 62 | 1.0.11 63 | 64 | 65 | 66 | com.alibaba 67 | fastjson 68 | 1.2.21 69 | 70 | 71 | 72 | org.projectlombok 73 | lombok 74 | 75 | 76 | 77 | 78 | 79 | 80 | org.springframework.boot 81 | spring-boot-maven-plugin 82 | 83 | true 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/App.java: -------------------------------------------------------------------------------- 1 | package com.lm; 2 | 3 | 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.cache.annotation.EnableCaching; 7 | 8 | @SpringBootApplication 9 | @EnableCaching 10 | public class App { 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(App.class, args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/config/RedisCacheConfig.java: -------------------------------------------------------------------------------- 1 | package com.lm.config; 2 | 3 | import com.fasterxml.jackson.annotation.JsonAutoDetect; 4 | import com.fasterxml.jackson.annotation.PropertyAccessor; 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import org.springframework.cache.CacheManager; 7 | import org.springframework.cache.annotation.CachingConfigurerSupport; 8 | import org.springframework.cache.annotation.EnableCaching; 9 | import org.springframework.cache.interceptor.KeyGenerator; 10 | import org.springframework.context.annotation.Bean; 11 | import org.springframework.context.annotation.Configuration; 12 | import org.springframework.data.redis.cache.RedisCacheManager; 13 | import org.springframework.data.redis.connection.RedisConnectionFactory; 14 | import org.springframework.data.redis.core.RedisTemplate; 15 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 16 | 17 | import java.lang.reflect.Method; 18 | 19 | /** 20 | * 21 | * @author liangming.deng 22 | * @date 2017年11月17日 23 | * 24 | */ 25 | @Configuration 26 | @EnableCaching 27 | public class RedisCacheConfig extends CachingConfigurerSupport { 28 | 29 | @Bean 30 | public KeyGenerator keyGenerator() { 31 | return new KeyGenerator() { 32 | @Override 33 | public Object generate(Object target, Method method, Object... params) { 34 | StringBuilder sb = new StringBuilder(); 35 | sb.append(target.getClass().getName()); 36 | sb.append(method.getName()); 37 | for (Object obj : params) { 38 | sb.append(obj.toString()); 39 | } 40 | return sb.toString(); 41 | } 42 | }; 43 | } 44 | 45 | 46 | @Bean 47 | public RedisTemplate redisTemplate(RedisConnectionFactory factory) { 48 | RedisTemplate template = new RedisTemplate<>(); 49 | template.setConnectionFactory(factory); 50 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 51 | ObjectMapper om = new ObjectMapper(); 52 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 53 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 54 | jackson2JsonRedisSerializer.setObjectMapper(om); 55 | template.setValueSerializer(jackson2JsonRedisSerializer); 56 | template.afterPropertiesSet(); 57 | return template; 58 | } 59 | 60 | @SuppressWarnings("rawtypes") 61 | @Bean 62 | public CacheManager cacheManager(RedisTemplate redisTemplate) { 63 | RedisCacheManager rcm = new RedisCacheManager(redisTemplate); 64 | //设置缓存过期时间,默认是0l 65 | rcm.setDefaultExpiration(60);//秒 66 | return rcm; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/controller/BaseController.java: -------------------------------------------------------------------------------- 1 | package com.lm.controller; 2 | 3 | import org.springframework.ui.ModelMap; 4 | 5 | /** 6 | * 7 | * @author liangming.deng 8 | * @date 2017年11月17日 9 | * 10 | */ 11 | public class BaseController { 12 | 13 | /** 14 | * 接口返回通用规范 15 | * @param code 16 | * @param msg 17 | * @param data 18 | * @return 19 | */ 20 | public ModelMap result(String code, String msg, Object data) { 21 | ModelMap modelMap = new ModelMap(); 22 | modelMap.put("code", code); 23 | modelMap.put("msg", msg); 24 | modelMap.put("result", data); 25 | return modelMap; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/controller/CacheRedisController.java: -------------------------------------------------------------------------------- 1 | package com.lm.controller; 2 | 3 | import java.nio.charset.StandardCharsets; 4 | import java.util.HashSet; 5 | import java.util.List; 6 | import java.util.Set; 7 | import java.util.concurrent.TimeUnit; 8 | 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.dao.DataAccessException; 11 | import org.springframework.data.redis.connection.RedisConnection; 12 | import org.springframework.data.redis.core.Cursor; 13 | import org.springframework.data.redis.core.RedisCallback; 14 | import org.springframework.data.redis.core.RedisTemplate; 15 | import org.springframework.data.redis.core.ScanOptions; 16 | import org.springframework.ui.ModelMap; 17 | import org.springframework.web.bind.annotation.PathVariable; 18 | import org.springframework.web.bind.annotation.RequestMapping; 19 | import org.springframework.web.bind.annotation.RequestMethod; 20 | import org.springframework.web.bind.annotation.RestController; 21 | 22 | import com.alibaba.fastjson.JSON; 23 | import com.alibaba.fastjson.JSONObject; 24 | import com.lm.entity.User; 25 | import com.lm.service.UserService; 26 | import com.lm.util.Constant; 27 | /** 28 | * 29 | * @author liangming.deng 30 | * @date 2017年11月17日 31 | * 32 | */ 33 | @RestController 34 | @RequestMapping(value = "/redis") 35 | public class CacheRedisController extends BaseController { 36 | 37 | @Autowired 38 | private UserService userService; 39 | 40 | @Autowired 41 | private RedisTemplate redisTemplate; 42 | 43 | private static final String REDIS_KEY_USER_PRIFIX = "user"; 44 | 45 | @RequestMapping(value = "/addUser", method = RequestMethod.GET) 46 | public ModelMap addAllUsersToRedis() { 47 | try { 48 | List userList = userService.selectAll(); 49 | for (User user : userList) { 50 | String jsonStr= JSONObject.toJSONString(user); 51 | redisTemplate.opsForValue().set(REDIS_KEY_USER_PRIFIX + user.getId(), jsonStr); 52 | } 53 | 54 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, null); 55 | } catch (Exception e) { 56 | e.printStackTrace(); 57 | } 58 | return result(Constant.FAIL_CODE, Constant.FAIL_MSG, null); 59 | } 60 | 61 | @RequestMapping(value = "/selectUser/{id}", method = RequestMethod.GET) 62 | public ModelMap selectUserById(@PathVariable int id) { 63 | try { 64 | String jsonStr = redisTemplate.opsForValue().get(REDIS_KEY_USER_PRIFIX + id); 65 | User user= JSONObject.toJavaObject(JSON.parseObject(jsonStr),User.class); 66 | 67 | if (null == user) { 68 | user = userService.selectByPrimaryKey(id); 69 | String valueJsonStr= JSONObject.toJSONString(user); 70 | redisTemplate.opsForValue().set(REDIS_KEY_USER_PRIFIX + user.getId(), valueJsonStr); 71 | } 72 | int tempTime = this.redisTemplate.getExpire("max").intValue(); 73 | this.redisTemplate.expire("max", tempTime, TimeUnit.SECONDS); 74 | 75 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, user); 76 | } catch (Exception e) { 77 | e.printStackTrace(); 78 | } 79 | return result(Constant.FAIL_CODE, Constant.FAIL_MSG, null); 80 | } 81 | 82 | @RequestMapping(value = "/flushCache", method = RequestMethod.GET) 83 | private ModelMap flushCache() throws Exception { 84 | try { 85 | Iterable execute = redisTemplate.execute(new RedisCallback>() { 86 | 87 | @Override 88 | public Iterable doInRedis(RedisConnection connection) throws DataAccessException { 89 | Set keys = new HashSet(); 90 | Cursor cursor = connection.scan(ScanOptions.scanOptions().count(5).build()); 91 | while (cursor.hasNext()) { 92 | byte[] key = cursor.next(); 93 | keys.add(new String(key, StandardCharsets.UTF_8)); 94 | connection.del(key); 95 | } 96 | return keys; 97 | } 98 | 99 | }); 100 | 101 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, execute); 102 | } catch (Exception e) { 103 | e.printStackTrace(); 104 | } 105 | return result(Constant.FAIL_CODE, Constant.FAIL_MSG, null); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.lm.controller; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Date; 5 | import java.util.List; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.cache.annotation.Cacheable; 11 | import org.springframework.data.redis.connection.RedisConnectionFactory; 12 | import org.springframework.ui.ModelMap; 13 | import org.springframework.web.bind.annotation.RequestBody; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | import org.springframework.web.bind.annotation.RequestMethod; 16 | import org.springframework.web.bind.annotation.RestController; 17 | 18 | import com.lm.entity.User; 19 | import com.lm.service.UserService; 20 | import com.lm.util.Constant; 21 | 22 | /** 23 | * 24 | * @author liangming.deng 25 | * @date 2017年11月17日 26 | * 27 | */ 28 | @RestController 29 | @RequestMapping("/user") 30 | 31 | public class UserController extends BaseController { 32 | 33 | private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class); 34 | 35 | @Autowired 36 | RedisConnectionFactory redisConnectionFactory; 37 | 38 | @Autowired 39 | private UserService userService; 40 | 41 | @RequestMapping(value = "/search", method = RequestMethod.POST) 42 | @Cacheable(value = "search", keyGenerator = "keyGenerator") 43 | public ModelMap search() { 44 | List userList = userService.selectAll(); 45 | LOGGER.info("search...."); 46 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, userList); 47 | } 48 | 49 | @RequestMapping(value = "/selectOne", method = RequestMethod.POST) 50 | @Cacheable(value = "search", keyGenerator = "keyGenerator") 51 | public ModelMap search(@RequestBody User user) { 52 | getUserIds(user); 53 | List userList = userService.selectByUsernameAndPassword(user); 54 | LOGGER.info("search...."); 55 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, userList); 56 | } 57 | 58 | private void getUserIds(@RequestBody User user) { 59 | List userIds = new ArrayList<>(); 60 | userIds.add(1); 61 | userIds.add(2); 62 | userIds.add(3); 63 | user.setUserIdList(userIds); 64 | } 65 | 66 | @RequestMapping(value = "/save", method = RequestMethod.POST) 67 | public ModelMap save(String username, String password) { 68 | User user = new User(username, password, "admin", new Date(), "admin", new Date()); 69 | user = userService.save(user); 70 | System.out.println(user); 71 | return result(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, 0); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/entity/User.java: -------------------------------------------------------------------------------- 1 | package com.lm.entity; 2 | 3 | import java.util.Date; 4 | import java.util.List; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.Table; 12 | import javax.persistence.Transient; 13 | 14 | import com.fasterxml.jackson.annotation.JsonFormat; 15 | 16 | import lombok.AllArgsConstructor; 17 | import lombok.Data; 18 | import lombok.NoArgsConstructor; 19 | import lombok.ToString; 20 | 21 | /** 22 | * lombok注解实现javabean 23 | * @author liangming.deng 24 | * @date 2017年11月17日 25 | * 26 | */ 27 | @Data 28 | @AllArgsConstructor 29 | @NoArgsConstructor 30 | @ToString 31 | @Entity 32 | @Table(name = "t_user") 33 | public class User { 34 | @Id 35 | @GeneratedValue(strategy = GenerationType.IDENTITY) 36 | private Integer id; 37 | 38 | @Column(name = "username") 39 | private String username; 40 | 41 | @Column(name = "password") 42 | private String password; 43 | 44 | @Column(name = "create_user") 45 | private String createUser; 46 | 47 | @Column(name = "create_time") 48 | @JsonFormat(pattern = "yyyy-mm-dd HH:mm:ss") 49 | private Date createTime; 50 | 51 | @Column(name = "update_user") 52 | private String updateUser; 53 | 54 | @Column(name = "update_time") 55 | @JsonFormat(pattern = "yyyy-mm-dd HH:mm:ss") 56 | private Date updateTime; 57 | 58 | @Transient 59 | private List userIdList; 60 | 61 | public User(String username, String password, String createUser, Date createTime, String updateUser, Date updateTime) { 62 | this.username = username; 63 | this.password = password; 64 | this.createUser = createUser; 65 | this.createTime = createTime; 66 | this.updateUser = updateUser; 67 | this.updateTime = updateTime; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.lm.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | import com.lm.entity.User; 8 | 9 | /** 10 | * 11 | * @author liangming.deng 12 | * @date 2017年11月17日 13 | * 14 | */ 15 | public interface UserRepository extends JpaRepository { 16 | List findByUsernameAndPassword(String username, String password); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.lm.service; 2 | 3 | 4 | import java.util.List; 5 | 6 | import com.lm.entity.User; 7 | 8 | /** 9 | * 10 | * @author liangming.deng 11 | * @date 2017年11月17日 12 | * 13 | */ 14 | public interface UserService { 15 | List selectAll(); 16 | 17 | User save(User user); 18 | 19 | List selectByUsernameAndPassword(User user); 20 | 21 | 22 | User selectByPrimaryKey(int id); 23 | } 24 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/service/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.lm.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.lm.entity.User; 9 | import com.lm.repository.UserRepository; 10 | 11 | /** 12 | * 13 | * @author liangming.deng 14 | * @date 2017年11月17日 15 | * 16 | */ 17 | @Service 18 | public class UserServiceImpl implements UserService { 19 | @Autowired 20 | private UserRepository userRepository; 21 | 22 | 23 | public List selectAll() { 24 | return userRepository.findAll(); 25 | } 26 | 27 | @Override 28 | public User save(User user) { 29 | return userRepository.save(user); 30 | } 31 | 32 | @Override 33 | public List selectByUsernameAndPassword(User user) { 34 | return userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword()); 35 | } 36 | 37 | @Override 38 | public User selectByPrimaryKey(int id) { 39 | return userRepository.findOne(id); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/java/com/lm/util/Constant.java: -------------------------------------------------------------------------------- 1 | package com.lm.util; 2 | 3 | /** 4 | * 5 | * @author liangming.deng 6 | * @date 2017年11月17日 7 | * 8 | */ 9 | public class Constant { 10 | public static final String SUCCESS_CODE = "200"; 11 | public static final String SUCCESS_MSG = "SUCCESS"; 12 | 13 | public static final String FAIL_CODE = "500"; 14 | public static final String FAIL_MSG = "服务异常请联系管理员"; 15 | } 16 | -------------------------------------------------------------------------------- /springboot-integ-redis/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8089 2 | # mysql 3 | spring.datasource.url=jdbc:mysql://172.16.3.207/spark_test 4 | spring.datasource.username=root 5 | spring.datasource.password=winit2015 6 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 7 | # Number of ms to wait before throwing an exception if no connection is available. 8 | spring.datasource.tomcat.max-wait=10000 9 | # Maximum number of active connections that can be allocated from this pool at the same time. 10 | spring.datasource.tomcat.max-active=50 11 | # Validate the connection before borrowing it from the pool. 12 | spring.datasource.tomcat.test-on-borrow=true 13 | 14 | 15 | # REDIS (RedisProperties) 16 | # Redis数据库索引(默认为0) 17 | spring.redis.database=0 18 | # Redis服务器地址 19 | spring.redis.host=127.0.0.1 20 | # Redis服务器连接端口 21 | spring.redis.port=6379 22 | # Redis服务器连接密码(默认为空) 23 | spring.redis.password= 24 | # 连接池最大连接数(使用负值表示没有限制) 25 | spring.redis.pool.max-active=8 26 | # 连接池最大阻塞等待时间(使用负值表示没有限制) 27 | spring.redis.pool.max-wait=-1 28 | # 连接池中的最大空闲连接 29 | spring.redis.pool.max-idle=8 30 | # 连接池中的最小空闲连接 31 | spring.redis.pool.min-idle=0 32 | # 连接超时时间(毫秒) 33 | spring.redis.timeout=0 34 | 35 | #日志开关 36 | server.tomcat.access-log-enabled=true 37 | #日志格式 38 | server.tomcat.access-log-pattern=%h %l %u %t "%r" %s %b %D 39 | #日志输出目录,这里是设置为当前目录下 40 | server.tomcat.basedir=./ 41 | spring.jpa.show-sql=true 42 | 43 | 44 | --------------------------------------------------------------------------------