SpringBoot配置mybatis


一直都說SpringBoot是零配置,當然,真正實現零配置是不可能的,但是在配置mybatis這里真的是太簡單了,哈哈,下面我們一起看一下。

1.先導入基於SpringBoot的mybatis依賴包

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
       <version>1.3.0</version> </dependency>

2.引入mybatis的基本配置文件mybatis.cfg.xml,這里就不過多做其他的配置了,一個最簡單的配置文件如下

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
        "http://mybatis.org/dtd/mybatis-3-config.dtd">  

<configuration>
    <!-- 進行Mybatis的相應的環境的屬性定義 -->
    <settings>   <!-- 在本項目中開啟二級緩存 -->
            <setting name="cacheEnabled" value="true"/>
    </settings>
</configuration>

3.在application.yml下配置mybatis相關。此配置主要是針對mybatis配置文件和mapper配置文件以及bean的路徑。

mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml      #mybatis配置文件所在路徑
  type-aliases-package: cn.mldn.microboot.vo              #定義所有操作類的別名所在包
  mapper-locations:                                       #所有的mapper映射文件
  - classpath:mybatis/mapper/**/*.xml

4.我們做一個Student對象的映射,實現基本的增刪改查功能,Student表和bean已經提前建好。

<?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="cn.mldn.microboot.dao.IStudentDAO">
    <select id="findAll" resultType="Student">
        SELECT * FROM student;
    </select>
    <insert id="doCreate" parameterType="Student">
        INSERT INTO    student(name,age,sex,birthday) VALUES(#{name},#{age},#{sex},#{birthday})
    </insert>
    <update id="doUpdate" parameterType="Student">
        UPDATE student set name = #{name} where id=#{id}
    </update>
    <delete id="doDelete" parameterType="int">
        Delete from student Where id=#{id}
    </delete>
</mapper>

這里的namespace屬性值對應我們Dao層,id則為方法名。先看一下Dao層的代碼。

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import cn.mldn.microboot.vo.Student;
@Mapper
public interface IStudentDAO {
        public List<Student> findAll();
        public boolean doCreate(Student vo);
        public boolean doUpdate(Student vo);
        public boolean doDelete(Integer id);
}

看到了,就是這么簡單,一個Mapper注解,解決了所有問題。

5.接下來我們寫一下Servic層和調用的測試類。

Service層接口定義如下:

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.mldn.microboot.vo.Student;

public interface IStudentService {
    @Transactional(readOnly = true)
    public List<Student> list();
    @Transactional(propagation=Propagation.REQUIRED)
    public boolean add(Student vo);
    @Transactional(propagation=Propagation.REQUIRED)
    public boolean update(Student vo);
    @Transactional(propagation=Propagation.REQUIRED)
    public boolean delete(Integer id);
}
Transactional注解是對事物的控制。
下面是Service的實現
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.mldn.microboot.dao.IStudentDAO;
import cn.mldn.microboot.service.IStudentService;
import cn.mldn.microboot.vo.Student;
@Service
public class StudentServiceImpl implements IStudentService {
    @Resource
    private IStudentDAO studentDao;
    @Override
    public List<Student> list() {
        return this.studentDao.findAll();
    }
    @Override
    public boolean add(Student vo) {
        return this.studentDao.doCreate(vo);
    }
    @Override
    public boolean update(Student vo) {
        return this.studentDao.doUpdate(vo);
    }
    @Override
    public boolean delete(Integer id) {
        return this.studentDao.doDelete(id);
    }
}

最后我們寫下測試類

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import cn.mldn.microboot.StartSpringBootMain;
import cn.mldn.microboot.service.IStudentService;
import cn.mldn.microboot.vo.Student;

@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestStudentService {
    @Resource
    private IStudentService studentService;
    @Test
    public void testList() throws Exception{
        System.out.println(this.studentService.list());
    }
    @Test
    public void testAdd() throws Exception{
        Student student = new Student();
        student.setAge(26);
        student.setName("來來來");
        student.setBirthday("2018/03/01");
        student.setSex(true);
        System.out.println(this.studentService.add(student));
    }
}

這里只寫了查詢和添加的測試類,測試

測試后,成功添加與查詢。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM