springBoot整合mybatis

 springBoot跟IBatis是啥我们也不介绍了,闲话不多说,主要看代码,

 1.搭建基本环境,创建一个maven工程,导入springBoot依赖的pom文件,


 4.0.0
    com.didispace
    Chapter1
    1.0.0
    jar

    spring-boot
    The first Spring Boot project

    
        org.springframework.boot
        spring-boot-starter-parent
        1.3.2.RELEASE
         
    

    
        UTF-8
        1.8
    
    
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
		
		
		 
		  org.springframework.boot
		  spring-boot-starter-freemarker
		

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2 .测试代码,看看能不能运行起来,在基包创建一个springBoot执行器

 

package cn.csdn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootThymeleafApp {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootThymeleafApp.class, args);
    }
}

 

3 .创建一个controller,需要注意的是,controller一定要在基包下,

package cn.csdn.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @RequestMapping("hello")
    @ResponseBody
    public String sayHello(){

        return "你好,程序猿";
    }
}

 

4 .运行执行器,页面测试,这里会有个坑,就是如果提前导入了myBatis需要的依赖就会报出

  Spring Boot - Cannot determine embedded database driver class for database type NONE

 

springBoot整合mybatis_第1张图片

 

 

 

5.执行到这里,环境已经基本算是ok的了,我们开始整合iBatis,导入iBatis所需要的依赖,以及数据库驱动



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.1.1



    mysql
    mysql-connector-java
    5.1.6

 

5.有俩种方案,一种是注解,一种是xml配置,注解是前期开发简单配置,但是后期开发就会显得有点无力了,

而且myBatis的优点就是动态sql,所以个人还是比较推荐使用xml配置的,

 

 建表

CREATE TABLE User(
uId INT PRIMARY KEY AUTO_INCREMENT,
uName VARCHAR(32),
uAge INT
);

  插入数据

INSERT INTO 
User

VALUES (NULL,'李白',16);INSERT INTO

User

VALUES (NULL,'杜甫',17);INSERT INTO

User

VALUES (NULL,'曹操',21);INSERT INTO

User

VALUES (NULL,'刘备',25);INSERT INTO

User

VALUES (NULL,'孙权',30);

 

 

 

创建pojo类(javaBean),

 

public class User {

	private Integer uId;

	private String uName;

	private Integer uAge;
	//要有set  get 方法..
}


 

   

 

 6.注解整合myBatis,

 1.创建一个mapper接口,

package cn.csdn.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
 

@Repository@Mapperpublic interface IUserDao { @Select("select * from User where uId = #{id}") public User selectById(Integer id);}

 

 

 

2.创建service接口

package cn.csdn.service;

public interface IUserService {
   User findById(Integer id);
}

 

 

 

3.创建service实现类

package cn.csdn.service;

import cn.csdn.dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao iUserDao;

    @Override
    public User findById(Integer id) {
        return iUserDao.selectById(id);
    }
}

 

3.编写controller

 @RequestMapping("test/{id}")@ResponseBody
    public User test(@PathVariable Integer id){

        return userService.findById(id);
    }

 

最后写完代码还要在resoures目录下增加配置文件application.properties

spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 1039191520

 最后在执行类里面添加注解,

@MapperScan("cn.csdn.dao")

 

springBoot整合mybatis_第2张图片
 

 

运行main方法,

 


  测试结果如下

springBoot整合mybatis_第3张图片

 

 

 

 

7.mapper通用方式

 1.pom文件添加依赖

  
			com.github.abel533
			mapper
			2.3.4
		
 
			com.github.abel533
			mapper
			2.3.4
		

2.在application.properties文件添加配置文件

#  别名扫描pojo,
mybatis.type-aliases-package=cn.csdn.pojo

# myBatis 配置文件的位置
mybatis.config-locations=classpath:SqlMapConfig.xml
#mapper xml的位置
mybatis.mapper-locations=classpath:mybatis/*Mapper.xml


在 resources 创建SqlMapConfig.xml 文件




   

3.编写mapper接口

package cn.csdn.dao;

import cn.csdn.pojo.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {

    User selectById(Integer id);
}

4.编写service层代码

@Autowired
    private UserMapper mapper;

    public User findById2(Integer id){
        return mapper.selectById(id);
    }


5.controller编写

 @RequestMapping("test2/{id}")
    @ResponseBody
    public User test2(@PathVariable Integer id){

        return userService.findById2(id);
    }


6.在resources 目录下创建mapper 文件夹,在文件夹里创建UserMapper.xml文件,请注意mapper的规范,这里就不提了





    

 

7.代码测试结果如下

springBoot整合mybatis_第4张图片


 

 


 

 

 


 


 

 


 

 

 

 

 

     

 

 

 

 

 

 

 

 

你可能感兴趣的:(springBoot整合mybatis)