SpringBoot和Mybatis整合

项目结构:

SpringBoot和Mybatis整合_第1张图片
1.application.properties中配置整合mybatis的配置文件、mybatis扫描别名的基本包与数据源

server.port=8088
#springboot   
mybatis.mapper-locations = classpath:mapper/UserMapper.xmlmybatis.config-location = classpath:mapper/sqlMapConfig.xml
mybatis.type-aliases-package= com.jianghao.bean
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = roo
tspring.datasource.password = root

2.pom.xml加入springboot整合mybatis的jar包与数据库驱动包

<project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0modelVersion>
    <groupId>parentprogroupId>    <artifactId>parentproartifactId>    <version>0.0.1-SNAPSHOTversion>    <parent>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-parentartifactId>        <version>1.5.2.RELEASEversion>    parent>    <dependencies>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-webartifactId>        dependency>
                <dependency>            <groupId>org.mybatis.spring.bootgroupId>            <artifactId>mybatis-spring-boot-starterartifactId>            <version>1.3.1version>        dependency>                <dependency>            <groupId>mysqlgroupId>            <artifactId>mysql-connector-javaartifactId>            <version>5.1.6version>        dependency>        <dependency>            <groupId>tk.mybatisgroupId>            <artifactId>mapper-spring-boot-starterartifactId>            <version>2.0.2version>        dependency>                <dependency>            <groupId>junitgroupId>            <artifactId>junitartifactId>            <version>4.9version>            <scope>testscope>        dependency>        <dependency>            <groupId>javax.servletgroupId>            <artifactId>servlet-apiartifactId>            <version>2.5version>            <scope>providedscope>        dependency>        <dependency>            <groupId>javax.servletgroupId>            <artifactId>jsp-apiartifactId>            <version>2.0version>            <scope>providedscope>        dependency>        <dependency>            <groupId>com.thoughtworks.xstreamgroupId>            <artifactId>xstreamartifactId>            <version>1.4.4version>        dependency>
    dependencies>
project>

mybatis全局配置文件:

sqlMapConfig.xml

<configuration>
configuration>

编写代码:

(1)User.java

public class USer implements Serializable {
    private static final long seriaVersionUID=1L;    
    private Integer id;   
    private String username;    
    private String password;
    public Integer getId() {        
        return id;    
    }
    public void setId(Integer id) {
        this.id = id;   
    }
    public String getUsername() {
        return username;    
    }
    public void setUsername(String username) {        
        this.username = username;    
    }
    public String getPassword() {
        return password;    
    }
    public void setPassword(String password) {        
        this.password = password;    
     }
 }

(2)UserMapper.java  (注意接口上的注解是@mapper,代替之前扫描接口的操作)

package com.jianghao.mapper;
import com.jianghao.bean.USer;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("userMapper")
public interface UserMapper  extends tk.mybatis.mapper.common.Mapper{

    public List<USer> findAll();
   }

UserMapper.xml


<mapper namespace="com.jianghao.mapper.UserMapper">    
  <select id="findAll" resultType="com.jianghao.bean.USer" >        
    select * from crm_user    
  select>
mapper>

(3)UserService.java

import com.jianghao.bean.USer;
import org.springframework.stereotype.Service;
import java.util.List;
public interface UserService {
    /**     * 根据接口查询所用的用户     */
        public List<USer> findAll();
}

UserServiceImpl.java

import com.jianghao.bean.USer;
import com.jianghao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{

    @Autowired   
    private UserMapper userMapper;
    public List<USer> findAll() {
            List<USer> list=userMapper.findAll();        
            return list;    
    }
}

(4)UserController.java

import com.jianghao.bean.USer;
import com.jianghao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController/**自动返回的是json格式数据***/
public class UserController {
    @Autowired    
    private UserService userService;
    @RequestMapping("list")    
    public List<USer> list(){
           List<USer> list= userService.findAll();       
           return list;    
    }
}

注意:@RestController 注解 (自动返回的是json格式数据)

SpringBoot和Mybatis整合_第2张图片
(5)测试类

@SpringBootApplication//Spring Boot项目的核心注解,主要目的是开启自动配置
@MapperScan(basePackages ={"com.jianghao.mapper"})
public class test1 {
    public static void main(String[] args) {
            SpringApplication.run(test1.class,args);
    }
}

4.启动测试

SpringBoot和Mybatis整合_第3张图片到此SpringBoot与Mybatis的整合完成!
最后附上数据表:

		Create Table

CREATE TABLE crm_user (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(20) DEFAULT NULL,
password varchar(20) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

你可能感兴趣的:(微服务,SpringBoot)