array(2) { ["docs"]=> array(10) { [0]=> array(10) { ["id"]=> string(3) "428" ["text"]=> string(77) "Visual Studio 2017 单独启动MSDN帮助(Microsoft Help Viewer)的方法" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(8) "DonetRen" ["tagsname"]=> string(55) "Visual Studio 2017|MSDN帮助|C#程序|.NET|Help Viewer" ["tagsid"]=> string(23) "[401,402,403,"300",404]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400964" ["_id"]=> string(3) "428" } [1]=> array(10) { ["id"]=> string(3) "427" ["text"]=> string(42) "npm -v;报错 cannot find module "wrapp"" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "zzty" ["tagsname"]=> string(50) "node.js|npm|cannot find module "wrapp“|node" ["tagsid"]=> string(19) "[398,"239",399,400]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400760" ["_id"]=> string(3) "427" } [2]=> array(10) { ["id"]=> string(3) "426" ["text"]=> string(54) "说说css中pt、px、em、rem都扮演了什么角色" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(12) "zhengqiaoyin" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400640" ["_id"]=> string(3) "426" } [3]=> array(10) { ["id"]=> string(3) "425" ["text"]=> string(83) "深入学习JS执行--创建执行上下文(变量对象,作用域链,this)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "Ry-yuan" ["tagsname"]=> string(33) "Javascript|Javascript执行过程" ["tagsid"]=> string(13) "["169","191"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511399901" ["_id"]=> string(3) "425" } [4]=> array(10) { ["id"]=> string(3) "424" ["text"]=> string(30) "C# 排序技术研究与对比" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "vveiliang" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(8) ".Net Dev" ["catesid"]=> string(5) "[199]" ["createtime"]=> string(10) "1511399150" ["_id"]=> string(3) "424" } [5]=> array(10) { ["id"]=> string(3) "423" ["text"]=> string(72) "【算法】小白的算法笔记:快速排序算法的编码和优化" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "penghuwan" ["tagsname"]=> string(6) "算法" ["tagsid"]=> string(7) "["344"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511398109" ["_id"]=> string(3) "423" } [6]=> array(10) { ["id"]=> string(3) "422" ["text"]=> string(64) "JavaScript数据可视化编程学习(二)Flotr2,雷达图" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "chengxs" ["tagsname"]=> string(28) "数据可视化|前端学习" ["tagsid"]=> string(9) "[396,397]" ["catesname"]=> string(18) "前端基本知识" ["catesid"]=> string(5) "[198]" ["createtime"]=> string(10) "1511397800" ["_id"]=> string(3) "422" } [7]=> array(10) { ["id"]=> string(3) "421" ["text"]=> string(36) "C#表达式目录树(Expression)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "wwym" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(4) ".NET" ["catesid"]=> string(7) "["119"]" ["createtime"]=> string(10) "1511397474" ["_id"]=> string(3) "421" } [8]=> array(10) { ["id"]=> string(3) "420" ["text"]=> string(47) "数据结构 队列_队列实例:事件处理" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "idreamo" ["tagsname"]=> string(40) "C语言|数据结构|队列|事件处理" ["tagsid"]=> string(23) "["246","247","248",395]" ["catesname"]=> string(12) "数据结构" ["catesid"]=> string(7) "["133"]" ["createtime"]=> string(10) "1511397279" ["_id"]=> string(3) "420" } [9]=> array(10) { ["id"]=> string(3) "419" ["text"]=> string(47) "久等了,博客园官方Android客户端发布" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(3) "cmt" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511396549" ["_id"]=> string(3) "419" } } ["count"]=> int(200) } 222 springboot 学习之路 3( 集成mybatis ) - 爱码网

 

目录:【持续更新。。。。。】
  
  spring boot 学习之路9 (项目启动后就执行特定方法)

 

下面就简单来说一下spring boot 与mybatiis的整合问题,如果你还没学习spring boot的注解的话,要先去看spring boot的注解

 

好了,现在让我们来搞一下与mybatis的整合吧,在整合过程中,我会把遇到的问题也说出来,希望可以让大家少走弯路!

  首先,是在pom.xml中添加一些依赖

  1. 这里用到spring-boot-starter基础和spring-boot-starter-test用来做单元测试验证数据访问
  2. 引入连接mysql的必要依赖mysql-connector-java
  3. 引入整合MyBatis的核心依赖mybatis-spring-boot-starter
  4. 这里不引入spring-boot-starter-jdbc依赖,是由于mybatis-spring-boot-starter中已经包含了此依赖

pom.xml部分依赖如下:

    

<!--lombok用于实体类,生成有参无参构造等只需要一个@Data注解就行-->
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <!--mybatis-->
        <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

注意,在上面依赖中,我多加了一个lombok的依赖,与本应用没多大关系,不要也是可以的,不影响项目运行。具体作用是实体类上通过注解来替代get/set   tosSring()等,具体用法参考lombok的简单介绍(1)

 

application.properties中配置mysql的连接配置:

    

 1 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring
 2 spring.datasource.username=root
 3 spring.datasource.password=123456
 4 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 5 spring.datasource.max-idle=10
 6 spring.datasource.max-wait=10000
 7 spring.datasource.min-idle=5
 8 spring.datasource.initial-size=5
 9 server.session.timeout=10
10 server.tomcat.uri-encoding=UTF-8
11 #连接mybatis 和在springmvc中一致,要制定mybatis配置文件的路径 上面是连接jdbcTemplate  相当于连接jdbc
12 # mybatis.config= classpath:mybatis-config.xml
13 #mybatis.mapperLocations=classpath:mappers/*.xml
14 
15 #mybatis.mapper-locations=classpath*:mappers/*Mapper.xml
16 #mybatis.type-aliases-package=com.huhy.web.entity
17 
18 spring.view.prefix=/WEB-INF/jsp/
19 spring.view.suffix=.jsp
20 
21 #设置端口
22 server.port=9999
23 #指定server绑定的地址
24 server.address=localhost
25 #设置项目访问根路径  不配置,默认为/
26 server.context-path=/

上面这些配置具体什么意义我就在这不多解释了,这些配置好之后,就可以搞代码开发了。

   首先创建一个User类:

    

 1 package com.huhy.web.entity;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 /**
 8  * Created by ${huhy} on 2017/8/5.
 9  */
10 @Data
11 @NoArgsConstructor
12 @AllArgsConstructor
13 public class User {
14     private String id;
15     private String name;
16     private int age;
17 }

实体类中我用到的注解是来自lombok中的,添加上@Data, @NoArgsConstructor, @AllArgsConstructor这几个注解,和我们写出来那些get/set,有参无参,toString 是一样的

UserMapper接口

    

 1 package com.huhy.web.mapper;
 2 
 3 import com.huhy.web.entity.User;
 4 import org.apache.ibatis.annotations.Insert;
 5 import org.apache.ibatis.annotations.Mapper;
 6 import org.apache.ibatis.annotations.Param;
 7 import org.apache.ibatis.annotations.Select;
 8 
 9 /**
10  * @Author:{huhy}
11  * @DATE:Created on/8/5 22:19
12  * @Class Description:
13  */
14 @Mapper
15 public interface UserMapper {
16     /*
17     * 通过id查询对应信息
18     * */
19     @Select("SELECT * FROM USER WHERE id = #{id}")
20     User selectByPrimaryKey(String id);
21     /*
22     * 通过name 查询对应信息
23     * */
24     @Select("Select * from user where name = #{name} and id = #{id}")
25     User selectByName(@Param("name") String name, @Param("id") String id);
26 
27     /**
28      * @param id
29      * @param name
30      * @param age
31      * @return
32      */
33     @Insert("INSERT INTO USER(id,NAME, AGE) VALUES(#{id},#{name}, #{age})")
34     int insert(@Param("id") String id,@Param("name") String name, @Param("age") Integer age);
35 }

在mapper通过注解编程写的,你也可以通过配置文件的形式,在.properties也有,只是被我注释了,现在统一用注解开发。

  Service

  

 1 package com.huhy.web.service.impl;
 2 
 3 import com.huhy.web.entity.User;
 4 import com.huhy.web.mapper.UserMapper;
 5 import com.huhy.web.service.UserService;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8 
 9 /**
10  * @Author:{huhy}
11  * @DATE:Created on 2017/8/5 22:18
12  * @Class Description:
13  */
14 @Service
15 public class UserServiceImpl implements UserService{
16 
17     @Autowired
18     private UserMapper userMapper;
19 
20     @Override
21     public User queryById(String id) {
22         User user = userMapper.selectByPrimaryKey(id);
23         return user;
24     }
25 
26     public User queryByName(String name,String id){
27         User user = userMapper.selectByName(name,id);
28         return user;
29     }
30 }

注意:在这有个问题:   通过UserMapper 的类上有两种注解可以使用:@Mapper   和 @Repository (我从网上查了一下,没发现有太大的差别,而且用不用都可以。我在使用中的唯一区别就是 @Repository注解,在service进行自动注入不会报warn,而@Mapper会报warn ,不管怎样不影响程序的运行)

       展示层(controller)

    

 1 package com.huhy.web.controller;
 2 
 3 import com.huhy.web.entity.User;
 4 import com.huhy.web.service.UserService;
 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 /**
11  * Created by ${huhy} on 2017/8/5.
12  */
13 
14 
15 @RestController
16 public class JDBCController {
17    /* @Autowired
18     private JdbcTemplate jdbcTemplate;
19     */
20     @Autowired
21     private UserService userService;
22 
23     /**
24      * 测试数据库连接
25      */
26     /*@RequestMapping("/jdbc/getUsers")
27     public List<Map<String, Object>> getDbType(){
28         String sql = "select * from user";
29         List<Map<String, Object>> list =  jdbcTemplate.queryForList(sql);
30         for (Map<String, Object> map : list) {
31             Set<Entry<String, Object>> entries = map.entrySet( );
32             if(entries != null) {
33                 Iterator<Entry<String, Object>> iterator = entries.iterator( );
34                 while(iterator.hasNext( )) {
35                     Entry<String, Object> entry =(Entry<String, Object>) iterator.next( );
36                     Object key = entry.getKey( );
37                     Object value = entry.getValue();
38                     System.out.println(key+":"+value);
39                 }
40             }
41         }
42         return list;
43     }*/
44 
45 
46     @RequestMapping("/jdbcTest/{id}")
47     public User jdbcTest(@PathVariable("id") String id){
48         return userService.queryById(id);
49     }
50 
51 
52     @RequestMapping("/jdbcTestName/{name}/{id}")
53     public User queryByName(@PathVariable("name") String name,@PathVariable("id") String id){
54         return userService.queryByName(name,id);
55     }
56     @RequestMapping("/name")
57     public  String getName(){
58         return "huhy";
59     }
60 
61 }

 

最后运行启动类就可以测试了:(在这有两中方式测试,第一种单独运行测试类,第二种就是junit测试)

  第一种:启动类启动

    

package com;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.huhy.web.mapper")//扫描dao接口
public class DemoApplication {
    /**
     *
     * @param 启动类
     */
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
}

上面@MapperScan后面跟的是我的mapper文件的包名。

 

第二种启动方式:junit测试(注意注解@SpringBootTest)

    

 1 package com.example.demo;
 2 
 3 import com.DemoApplication;
 4 import com.huhy.web.entity.User;
 5 import com.huhy.web.mapper.UserMapper;
 6 import org.junit.Test;
 7 import org.junit.runner.RunWith;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.test.annotation.Rollback;
11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12 import org.springframework.test.context.web.WebAppConfiguration;
13 
14 @RunWith(SpringJUnit4ClassRunner.class) // SpringJUnit支持,由此引入Spring-Test框架支持!                         @RunWith(SpringRunner.clss)
15 @SpringBootTest(classes = DemoApplication.class) // 指定我们SpringBoot工程的Application启动类                       @SpringBootTest 
16 @WebAppConfiguration // 由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
17 public class DemoApplicationTests {
18     @Autowired
19     private UserMapper userMapper;
20     @Test
21     @Rollback
22     public void findByName() throws Exception {
23         userMapper.insert("17","def",123);
24         User user = userMapper.selectByName("def","17");
25         System.out.println(user);
26     }
27 }

 

注意 :@SpringBootTest注解是在1.4版本之后才有的,原来是SpringApplicationConfiguration。在使用junit测试时,具体看看你的sprin-boot-test的版本

      如果你遇到SpringApplicationConfiguration 不能使用那就是以为 Spring Boot的SpringApplicationConfiguration注解在Spring Boot 1.4开始,被标记为Deprecated

    解决:替换为SpringBootTest即可

 

 

 下一篇:spring boot 学习之路4(日志输出)

快速构建demo

相关文章: