之前一直在用springMVC,接触到springboot之后,感觉使用起来方便多了,没那多xml需要配置。

先来看看整个项目结构,当然是maven项目。

1、测试数据

  1. DROP TABLE IF EXISTS `student`;
  2. CREATE TABLE `student` (
  3. `id` int(11) NOT NULL AUTO_INCREMENT,
  4. `name` varchar(100) NOT NULL,
  5. `sex` varchar(100) NOT NULL,
  6. `no` int(100) NOT NULL,
  7. `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  8. `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
  11.  
  12. -- ----------------------------
  13. -- Records of student
  14. -- ----------------------------
  15. INSERT INTO `student` VALUES ('', 'danny', 'male', '', '2017-06-12 11:12:30', '2017-06-12 13:21:12');
  16. INSERT INTO `student` VALUES ('', 'ellen', 'female', '', '2017-06-12 11:12:50', '2017-06-12 13:21:19');

2、pom.xml 文件

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>learn.danny.yao</groupId>
  5. <artifactId>springboot-demo</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7.  
  8. <parent>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-parent</artifactId>
  11. <version>2.0.0.M1</version>
  12. </parent>
  13.  
  14. <properties>
  15. <java.version>1.8</java.version>
  16. <mybatis.version>3.4.4</mybatis.version>
  17. <druid.version>1.0.18</druid.version>
  18. <mybatisSpring.version>1.3.0</mybatisSpring.version>
  19. </properties>
  20.  
  21. <dependencies>
  22. <!--Spring Boot -->
  23. <!--支持 Web 应用开发,包含 Tomcat 和 spring-mvc。 -->
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <!--支持使用 JDBC 访问数据库 -->
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-jdbc</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>com.alibaba</groupId>
  35. <artifactId>druid</artifactId>
  36. <version>${druid.version}</version>
  37. </dependency>
  38. <!--Mybatis -->
  39. <dependency>
  40. <groupId>org.mybatis</groupId>
  41. <artifactId>mybatis-spring</artifactId>
  42. <version>${mybatisSpring.version}</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.mybatis</groupId>
  46. <artifactId>mybatis</artifactId>
  47. <version>${mybatis.version}</version>
  48. </dependency>
  49. <!--Mysql / DataSource -->
  50. <dependency>
  51. <groupId>org.apache.tomcat</groupId>
  52. <artifactId>tomcat-jdbc</artifactId>
  53. </dependency>
  54. <dependency>
  55. <groupId>mysql</groupId>
  56. <artifactId>mysql-connector-java</artifactId>
  57. </dependency>
  58. <!--Json Support -->
  59. <!-- <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId>
  60. <version>1.1.43</version> </dependency> -->
  61. <!--Swagger support -->
  62. <!-- <dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId>
  63. <version>0.9.5</version> </dependency> -->
  64.  
  65. <!-- 热部署 -->
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>springloaded</artifactId>
  69. <version>1.2.5.RELEASE</version>
  70. </dependency>
  71. </dependencies>
  72.  
  73. <build>
  74. <plugins>
  75. <plugin>
  76. <groupId>org.springframework.boot</groupId>
  77. <artifactId>spring-boot-maven-plugin</artifactId>
  78. </plugin>
  79. </plugins>
  80. </build>
  81. </project>

3、springboot入口类 Application.java

  1. package demo.springboot;
  2.  
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7.  
  8. @SpringBootApplication
  9. public class Application {
  10.  
  11. private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
  12.  
  13. public static void main(String[] args) {
  14. SpringApplication.run(Application.class, args);
  15. LOGGER.info("Springboot started successfully!");
  16. }
  17. }

4、Model类 Student.java

  1. package demo.springboot.model;
  2.  
  3. import java.util.Date;
  4.  
  5. public class Student {
  6. private Integer id;
  7.  
  8. private String name;
  9.  
  10. private String sex;
  11.  
  12. private Integer no;
  13.  
  14. private Date createTime;
  15.  
  16. private Date updateTime;
  17.  
  18. public Integer getId() {
  19. return id;
  20. }
  21.  
  22. public void setId(Integer id) {
  23. this.id = id;
  24. }
  25.  
  26. public String getName() {
  27. return name;
  28. }
  29.  
  30. public void setName(String name) {
  31. this.name = name;
  32. }
  33.  
  34. public String getSex() {
  35. return sex;
  36. }
  37.  
  38. public void setSex(String sex) {
  39. this.sex = sex;
  40. }
  41.  
  42. public Integer getNo() {
  43. return no;
  44. }
  45.  
  46. public void setNo(Integer no) {
  47. this.no = no;
  48. }
  49.  
  50. public Date getCreateTime() {
  51. return createTime;
  52. }
  53.  
  54. public void setCreateTime(Date createTime) {
  55. this.createTime = createTime;
  56. }
  57.  
  58. public Date getUpdateTime() {
  59. return updateTime;
  60. }
  61.  
  62. public void setUpdateTime(Date updateTime) {
  63. this.updateTime = updateTime;
  64. }
  65.  
  66. }

5、mapper类,StudentMapper.java

  1. package demo.springboot.mapper;
  2.  
  3. import java.util.List;
  4.  
  5. import demo.springboot.model.Student;
  6.  
  7. public interface StudentMapper {
  8.  
  9. public List<Student> listStudents();
  10.  
  11. }

6、src/main/resources/mybatis/mapper/StudentMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="demo.springboot.mapper.StudentMapper" >
  4. <resultMap id="BaseResultMap" type="demo.springboot.model.Student" >
  5. <id column="id" property="id" jdbcType="INTEGER" />
  6. <result column="name" property="name" jdbcType="VARCHAR" />
  7. <result column="sex" property="sex" jdbcType="VARCHAR" />
  8. <result column="no" property="no" jdbcType="INTEGER" />
  9. <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
  10. <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  11. </resultMap>
  12. <sql id="Base_Column_List" >
  13. id, name, sex, no, create_time, update_time
  14. </sql>
  15. <select id="listStudents" resultMap="BaseResultMap">
  16. select
  17. <include refid="Base_Column_List" />
  18. from student
  19. </select>
  20. </mapper>

7、service类,StudentService.java

  1. package demo.springboot.service;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8.  
  9. import demo.springboot.mapper.StudentMapper;
  10. import demo.springboot.model.Student;
  11.  
  12. @Service
  13. public class StudentService {
  14.  
  15. @Autowired
  16. private StudentMapper studentMapper;
  17.  
  18. public List<Student> listStudents(){
  19. List<Student> students = new ArrayList<>();
  20. students = studentMapper.listStudents();
  21. return students;
  22. }
  23.  
  24. }

8、mybatis配置类,主要配置DataSource和SqlSessionFactory。MybatisConfig.java

  1. package demo.springboot.conf;
  2.  
  3. import java.util.Properties;
  4. import javax.sql.DataSource;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.mybatis.spring.SqlSessionFactoryBean;
  7. import org.mybatis.spring.annotation.MapperScan;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.core.env.Environment;
  12. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  13. import com.alibaba.druid.pool.DruidDataSourceFactory;
  14.  
  15. /**
  16. *
  17. * @author danny.yao
  18. * springboot集成mybatis基本入口
  19. * 1、创建数据源
  20. * 2、创建SqlSessionFactory
  21. */
  22. @Configuration
  23. @MapperScan(basePackages="demo.springboot.mapper")
  24. public class MybatisConfig {
  25.  
  26. @Autowired
  27. Environment environment;
  28.  
  29. /**
  30. * 1、创建数据源
  31. * @throws Exception
  32. * @Primary该注解表示在同一个接口有多个类可以注入的时候,默认选择哪个,而不是让@Autowired报错
  33. */
  34. @Bean
  35. // @Primary
  36. public DataSource getDataSource() throws Exception{
  37. Properties properties = new Properties();
  38. properties.put("driverClassName", environment.getProperty("jdbc.driverClassName"));
  39. properties.put("url", environment.getProperty("jdbc.url"));
  40. properties.put("username", environment.getProperty("jdbc.username"));
  41. properties.put("password", environment.getProperty("jdbc.password"));
  42. return DruidDataSourceFactory.createDataSource(properties);
  43. }
  44.  
  45. /**
  46. * 2、根据数据源创建SqlSessionFactory
  47. * @throws Exception
  48. */
  49. @Bean
  50. public SqlSessionFactory sessionFactory(DataSource dataSource) throws Exception{
  51. SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
  52. sessionFactoryBean.setDataSource(dataSource);
  53. sessionFactoryBean.setTypeAliasesPackage(environment.getProperty("mybatis.typeAliasesPackage"));
  54. sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(environment.getProperty("mybatis.mapperLocations")));
  55. return sessionFactoryBean.getObject();
  56. }
  57.  
  58. }

9、src/main/resources/application.properties 配置文件

  1. jdbc.driverClassName = com.mysql.jdbc.Driver
  2. jdbc.url = jdbc:mysql://127.0.0.1:3306/test
  3. jdbc.username = root
  4. jdbc.password = test
  5.  
  6. # mybatis #
  7. mybatis.typeAliasesPackage=demo.springboot.model
  8. mybatis.mapperLocations=classpath:/mybatis/mapper/*.xml

10、至此,文件就完整了,启动应用

Application.java类上 run as Java Application,启动应用。看到“Springboot started successfully!” 说明启动成功

11、测试

从浏览器访问 http://localhost:8080/student/list,查看日志看能否正常打印db返回的信息

DEMO: springboot 与 mybatis 集成的更多相关文章

  1. DEMO: springboot 与 freemarker 集成

    直接在 DEMO: springboot 与 mybatis 集成 基础上,进行修改. 1.pom.xml 中引用 依赖 <dependency> <groupId>org.s ...

  2. Springboot 和 Mybatis集成开发

    Springboot 和 Mybatis集成开发 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 jdk:1.7.0_79 maven:3.3.9 额外功能 PageHel ...

  3. springboot和mybatis集成

    springboot和mybatis集成 pom  <?xml version="1.0" encoding="UTF-8"?> <proje ...

  4. springboot和mybatis集成,自动生成model、mapper,增加mybatis分页功能

    整体思路和http://www.cnblogs.com/mahuan2/p/5859921.html相同. 主要讲maven的pom.xml和一些配置变化,详细说明. 软件简介 Spring是一个流行 ...

  5. springboot与mybatis集成

    1)添加依赖 <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId& ...

  6. SpringBoot 整合 MyBatis,实现 CRUD 示例

    目录 前言 创建项目/模块 SpringBoot Console Application CommandLineRunner SpringBoot 集成 MyBatis 创建数据库/表 配置数据源/连 ...

  7. SpringBoot框架与MyBatis集成,连接Mysql数据库

    SpringBoot是一种用来简化新Spring应用初始搭建及开发过程的框架,它使用特定方式来进行配置,使得开发人员不再需要定义样板化的配置.MyBatis是一个支持普通SQL查询.存储和高级映射的持 ...

  8. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  9. spring-boot 速成(8) 集成druid+mybatis

    spring-boot与druid.mybatis集成(包括pageHelper分页插件), 要添加以下几个依赖项: compile('mysql:mysql-connector-java:6.0.5 ...

随机推荐

  1. 【CQOI2008】中位数

    题不难,但是思路有意思,这个是我自己想出来的OvO 原题: 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. n<= ...

  2. CAM 查看里先选哪些层才能方便查看

    CAM 检查 Gerber 时选 Layer 时有先后次序,才以看清楚是否有冲突. 比如检查 TOP 层时顺序应该是 MT ST L1 BOT 层检查顺序 MB SB L2/L4

  3. 【RAC】使用一条“ps”命令获取Linux环境下全部RAC集群进程信息

    如何仅使用一条ps命令便能获取到所有与RAC集群进程相关的信息.  从所使用的命令角度上看很简单,仅需使用ps命令结合grep命令便能实现.问题关键是需要确定检索哪些关键字. 1.与RAC集群有关的进 ...

  4. 修改Nginx的header伪装服务器

    有时候为了伪装自己的真实服务器环境.不像让对方知道自己的webserver真实环境,就不得不修改我们的webserer软件了!今天看了一下baidu.com的webserver感觉像是nginx修改的 ...

  5. WebClient类

    WebClient类提供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法. 其实就相当于创建一个请求客户端.可以获取网页和各种各样的信息,包括交互. 通过MSDN来看看WebC ...

  6. dzzoffice协同办公平台与onlyoffice在线协作平台安装与部署

    1.安装dzzoffice协同办公平台 DzzOffice是一套开源办公套件,适用于企业.团队搭建自己的 类似“Google企业应用套件”.“微软Office365”的企业协同办公平台. 官网地址:h ...

  7. php中二维数组排序问题方法详解

    PHP中二维数组排序,可以使用PHP内置函数uasort() 示例一: 使用用户自定义的比较函数对数组中的值进行排序并保持索引关联 回调函数如下:注意回调函数的返回值是负数或者是false的时候,表示 ...

  8. 利用Python玩微信跳一跳

    创建python项目jump_weixin,新建python程序jump.py 需要4个辅助文件[adb.exe,AdbWinApi.dll,AdbWinUsbApi.dll,fastboot.exe ...

  9. 转 WCF中同步和异步通讯总结

    我这样分个类: WCF中, 以同步.异步角度考虑通讯的方式分为四种:跨进程同步.跨进程异步.发送队列端同步.发送队列端异步.之所以造成这样的结果源于两个因素,一个是传统概念上的同异步,一个是对于WCF ...

  10. 轻松理解execl系列函数

    execl函数功能如下:启动一个可执行文件,并且对他进行传送参数.一些原型如下 #include <unistd.h> extern char **environ; int execl(c ...