spring boot 整合mybatis

来源:互联网 发布:打码刷码软件官方下载 编辑:程序博客网 时间:2024/06/09 23:01
spring boot 整合mybatis 很简单 而且配置好后直接就可以使用,其他配置,如果是自己普通的整合,那会有一大堆繁琐的配置。

通过maven方式引入依赖


<dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.1.1</version>        </dependency> <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.21</version>        </dependency>


 除了引入mybatis支持之外,因为我使用的是mysql,所以还要引入mysql支持。
导入完后,在application.properties里面配置一下


spring.datasource.url=****spring.datasource.username=****spring.datasource.password=****spring.datasource.driver-class-name=com.mysql.jdbc.Drivermybatis.config-location=classpath:mybatis-config.xmlmybatis.mapper-locations=classpath:mapper/*Mapper.xml  

后面两个是mybatis配置文件以及映射文件,都应该放在resourses文件夹下。
 配置完之后就可以直接使用了


package com.kiruma.springboottest.controller;import com.kiruma.springboottest.domain.User;import com.kiruma.springboottest.mapper.UserMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/home")public class UserController {    @Autowired    UserMapper userMapper;    @RequestMapping("/user")    public String user(){        User user=userMapper.findUserByName("kiruma");        return user.toString();    }}package com.kiruma.springboottest.domain;public class User {    private int user_id;    private String name;    private String password;    public int getUser_id() {        return user_id;    }    public void setUser_id(int user_id) {        this.user_id = user_id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}package com.kiruma.springboottest.mapper;import com.kiruma.springboottest.domain.User;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;@Mapperpublic interface UserMapper {        @Select("select * from user where user_id=#{id}")        User findUserById(@Param("id") int id);        User findUserByName(@Param("name") String name);}


映射文件:


<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.kiruma.springboottest.mapper.UserMapper">    <select id="findUserByName" resultType="user" parameterType="String">        SELECT * FROM user WHERE  name=#{name}    </select></mapper>


这样子就可以了。
下面看看普通方式除了配置数据库账户,密码之后还要配置什么呢。


首先是sqlSessionFactory,生成sqlSession的工厂,配置它需要mybatis配置文件(必须) 和mapper映射的路径。



<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean>


如果mapper接口不使用@Mapper,还要配置自动扫描mapper接口


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.kiruma.springboottest.mapper"></property></bean>