1、添加依赖

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>1.1.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>commons-dbcp</groupId>
  8. <artifactId>commons-dbcp</artifactId>
  9. <version>1.4</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>mysql</groupId>
  13. <artifactId>mysql-connector-java</artifactId>
  14. </dependency>

2、在src/main/resources建立config目录,创建db.properties:

  1. #jdbc
  2. jdbc.driver=com.mysql.jdbc.Driver
  3. jdbc.url=jdbc:mysql://118.25.190.197:3306/test?useUnicode=true&characterEncoding=utf8
  4. jdbc.username=root
  5. jdbc.password=******
  6. jdbc.maxActive=2335
  7. jdbc.maxIdel=120
  8. jdbc.maxWait=100

3、在src/main/java创建包cn.mmweikt.config.database,创建3个类:

  1. package cn.mmweikt.producer.config.database;
  2. import org.apache.commons.dbcp.BasicDataSource;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.PropertySource;
  7. @Configuration
  8. //这个注解导入刚才增加的jdbc配置文件
  9. @PropertySource("classpath:config/db.properties")
  10. public class DataSourceConfiguration {
  11. @Value("${jdbc.driver}")
  12. private String driver;
  13. @Value("${jdbc.url}")
  14. private String url;
  15. @Value("${jdbc.username}")
  16. private String username;
  17. @Value("${jdbc.password}")
  18. private String password;
  19. @Value("${jdbc.maxActive}")
  20. private int maxActive;
  21. @Value("${jdbc.maxIdel}")
  22. private int maxIdel;
  23. @Value("${jdbc.maxWait}")
  24. private long maxWait;
  25. @Bean
  26. public BasicDataSource dataSource(){
  27. BasicDataSource dataSource = new BasicDataSource();
  28. dataSource.setDriverClassName(driver);
  29. dataSource.setUrl(url);
  30. dataSource.setUsername(username);
  31. dataSource.setPassword(password);
  32. dataSource.setMaxActive(maxActive);
  33. dataSource.setMaxIdle(maxIdel);
  34. dataSource.setMaxWait(maxWait);
  35. dataSource.setValidationQuery("SELECT 1");
  36. dataSource.setTestOnBorrow(true);
  37. dataSource.setDefaultAutoCommit(true);
  38. return dataSource;
  39. }
  40. }
  1. package cn.mmweikt.producer.config.database;
  2. import javax.sql.DataSource;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.mybatis.spring.SqlSessionFactoryBean;
  5. import org.mybatis.spring.SqlSessionTemplate;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  10. import org.springframework.transaction.PlatformTransactionManager;
  11. import org.springframework.transaction.annotation.EnableTransactionManagement;
  12. import org.springframework.transaction.annotation.TransactionManagementConfigurer;
  13. /**
  14. * @author kibana
  15. *
  16. */
  17. @Configuration
  18. //加上这个注解,使得支持事务
  19. @EnableTransactionManagement
  20. public class MyBatisConfig implements TransactionManagementConfigurer {
  21. @Autowired
  22. private DataSource dataSource;
  23. @Override
  24. public PlatformTransactionManager annotationDrivenTransactionManager() {
  25. return new DataSourceTransactionManager(dataSource);
  26. }
  27. @Bean(name = "sqlSessionFactory")
  28. public SqlSessionFactory sqlSessionFactoryBean() {
  29. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  30. bean.setDataSource(dataSource);
  31. try {
  32. return bean.getObject();
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. throw new RuntimeException(e);
  36. }
  37. }
  38. @Bean
  39. public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
  40. return new SqlSessionTemplate(sqlSessionFactory);
  41. }
  42. }
  1. package cn.mmweikt.producer.config.database;
  2. import org.mybatis.spring.mapper.MapperScannerConfigurer;
  3. import org.springframework.boot.autoconfigure.AutoConfigureAfter;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. /**
  7. * 扫描mybatis的接口
  8. *
  9. * @author kibana
  10. *
  11. */
  12. @Configuration
  13. // 因为这个对象的扫描,需要在MyBatisConfig的后面注入,所以加上下面的注解
  14. @AutoConfigureAfter(MyBatisConfig.class)
  15. public class MyBatisMapperScannerConfig {
  16. @Bean
  17. public MapperScannerConfigurer mapperScannerConfigurer() {
  18. MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
  19. //获取之前注入的beanName为sqlSessionFactory的对象
  20. mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
  21. //指定xml配置文件的路径
  22. mapperScannerConfigurer.setBasePackage("cn.mmweikt.mapping");
  23. return mapperScannerConfigurer;
  24. }
  25. }

3、创建cn.mmweikt.mapper包,添加接口:

  1. package cn.mmweikt.producer.mapper;
  2. import java.util.List;
  3. import java.util.Map;
  4. import cn.mmweikt.producer.entity.Order;
  5. import org.apache.ibatis.annotations.Insert;
  6. import org.apache.ibatis.annotations.Mapper;
  7. import org.apache.ibatis.annotations.Param;
  8. import org.apache.ibatis.annotations.Select;
  9. @Mapper
  10. //在这里,使用@Mapper注解来标识一个接口为MyBatis的接口,MyBatis会自动寻找这个接口
  11. public interface TestDao {
  12. //@Select("select * from wx_userinfo;")
  13. //public Mapfind();
  14. //@Insert("insert into wx_userinfo(openid,status,nickname,sex,city,province,country,headimgurl,subscribe_time) "+
  15. // "values(#{id},1,'nick',1,'city','provi','contr','img',now())")
  16. //public int insert(@Param("id")int id);
  17. @Select("select id,name as body,message_id as messageId from t_order")
  18. public List<Order> queryAll();
  19. }

4、创建数据库表t_order(id,name,message_id)和与之对应的实体类,cn.mmweikt.entity.Order:

  1. -- ----------------------------------
  2. -- Table structure for t_order
  3. -- ----------------------------------
  4. DROP TABLE IF EXISTS `t_order`;
  5. CREATE TABLE `t_order` (
  6. `id` varchar(128) NOT NULL,
  7. `name` varchar(128) DEFAULT NULL,
  8. `message_id` varchar(128) NOT NULL,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. package cn.mmweikt.producer.entity;
  2. import java.io.Serializable;
  3. public class Order implements Serializable {
  4. private static final long serialVersionUID = -5803120402049000020L;
  5. private String id;
  6. private String messageId;
  7. private String body;
  8. public String getId() {
  9. return id;
  10. }
  11. public void setId(String id) {
  12. this.id = id;
  13. }
  14. public String getMessageId() {
  15. return messageId;
  16. }
  17. public void setMessageId(String messageId) {
  18. this.messageId = messageId;
  19. }
  20. public String getBody() {
  21. return body;
  22. }
  23. public void setBody(String body) {
  24. this.body = body;
  25. }
  26. }

5、创建cn.mmweikt.controller测试。

Springboot+mybatis+dbcp+mysql简单集成的更多相关文章

  1. Java逆向工程SpringBoot + Mybatis Generator + MySQL

    Java逆向工程SpringBoot+ Mybatis Generator + MySQL Meven pop.xml文件添加引用: <dependency> <groupId> ...

  2. SpringBoot+Mybatis+Maven+MySQL逆向工程实现增删改查

    SpringBoot+Mybatis+MySQL+MAVEN逆向工程实现增删改查 这两天简单学习了下SpringBoot,发现这玩意配置起来是真的方便,相比于SpringMVC+Spring的配置简直 ...

  3. 记一次springboot+mybatis+phoenix在代码集成中的坑

    场景: 希望使用phoenix做查询服务,给服务端提供接口 设计: 通过springboot做restful的接口发布,通过mybatis做phoenix的sql处理,因此是springboot+my ...

  4. SpringBoot+Mybatis+Freemark 最简单的例子

    springboot-sample 实现最简单的 SpringBoot + Mybatis + Freemarker 网页增删改查功能,适合新接触 Java 和 SpringBoot 的同学参考 代码 ...

  5. 【时区问题】SpringBoot+mybatis查询mysql的datetime类型数据时间差14小时

    [时区问题]MyBatis查询MySQL的datetime类型数据时间差14小时 故障解决方式 与数据库连接时,定义时区,避免mybatis框架从mysql获取时区.在连接上加上 serverTime ...

  6. Spring-boot+Mybatis+Maven+MySql搭建实例

    转自:https://www.jianshu.com/p/95fb7be049ae 最近读了spring-boot开发手册,spring-boot相比于spring-mvc封装了很多常用的依赖,并且内 ...

  7. 通用mapper版+SpringBoot+MyBatis框架+mysql数据库的整合

    转:https://blog.csdn.net/qq_35153200/article/details/79538440 开发环境: 开发工具:Intellij IDEA 2017.2.3 JDK : ...

  8. Solr与Mysql简单集成

    Solr与Mysql数据库的集成,实现全量索引.增量索引的创建. 基本原理很简单:在Solr项目中注册solr的DataImportHandler并配置Mysql数据源以及数据查询sql语句.当我们通 ...

  9. 7、SpringBoot+Mybatis整合------PageHelper简单分页

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/1d30d2a573ce6784149a28af9b ...

随机推荐

  1. Cocos2d-x之String

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 在Cocos2d-x中能够使用的字符串constchar*.std::string和cocos2d::__String等,其中const ...

  2. centos7 安装 gitlab 服务器

    https://www.cnblogs.com/wenwei-blog/p/5861450.html 标签: centos7 git安装 git gitlab 我这里使用的是centos 7 64bi ...

  3. log4j基础配置使用

    添加log4j的jar包:可以从maven处下载:https://mvnrepository.com/artifact/log4j/log4j/1.2.17 <!-- https://mvnre ...

  4. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  5. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  6. static的变量是放在哪里

    static的变量都放在数据段,但是初始值若为0则放在BSS节中.而初始值非零则放在数据节中. 数据节和BSS节都属于数据段.   顺便说说对象的存储,可分为三类:静态存储(static storag ...

  7. jsp与httpservlet的微小区别

    2015-8 jsp与httpservlet的微小区别: jsp默认支持会话,httpservlet默认不支持会话:jsp: 可以直接通过session引用httpservlet对象httpservl ...

  8. java的几种定时器

    https://blog.csdn.net/coolwindd/article/details/82804189 1.@Scheduled注解 @Scheduled注解是最简单的方式,只需要启用定时器 ...

  9. iOS 点击按钮截屏

    @interface CaptureViewController () @property (nonatomic, strong) UIImageView *backgrounView; //控制器背 ...

  10. jenkins参数化配置,pom.xml配置

    1.要实现Jenkins参数化构建,要先在代码里写好能接收该参数value的配置,在pom.xml文件里加配置,如下: 1)<properties></properties>里 ...