세부 봄 부팅 통합의 MyBatis 구성

먼저, 봄 부트 프로젝트를해야합니다.

 

보통 일반적인 저장소 패키지의 MyBatis 년 매퍼를 대체되었습니다 개발할 수 있습니다.

 

 

구성 :

1. 의존도 소개 :

< 의존성 > 
  < 의 groupId > org.mybatis.spring.boot </ 의 groupId > 
   < artifactId를 > 의 MyBatis 스프링 부팅 스타트 </ artifactId를 > 
   < 버전 > 1.1.1 </ 버전 > 
</ 의존성 >

 

2. 편집 application.properties

spring.datasource.url = JDBC한다 : mysql : // ? 로컬 호스트 : 3306 / 테스트 useSSL = TRUE & serverTimezone = GMT 
spring.datasource.username = 루트 
spring.datasource.password = 루트 
spring.datasource.driver - 클래스 -name = com.mysql .jdbc.Driver 

spring.servlet.multipart.max -request 크기 = 2천50메가바이트 
spring.servlet.multipart.max - 파일 크기 = 2,048메가바이트의 

spring.jpa.hibernate.use - 새로운 -id 발전기 - 매핑 = 거짓 
server.port = 8080 
server.servlet.context -path = / mybatisDemo 
spring.jmx.enabled = 거짓

mybatis.type -aliases- 패키지 = tgc.edu.wx.entity의 
mybatis.mapperLocations = 클래스 경로 : 매핑 / * .XML

여기서 우리는 두 번째는 주소 매핑 파일, 두 가지 구성 MyBatis로, 패키지의 한 스캔을주의해야합니다.

 

다음 그림의 완료 후, 원하는 웹 항목 클래스, 엔티티 클래스와 데이터베이스의 해당 테이블의 설립을 수립 3. 건축 :

 

PeopleMapper.java

패키지 tgc.edu.wx.mapper; 

수입 은 java.util.List; 

수입 org.apache.ibatis.annotations.Mapper; 

수입 tgc.edu.wx.entity.People; 

@Mapper 
공용  인터페이스 PeopleMapper { 

    공개 목록 <사람> findall은 (); 
}

이 파일은 다음과 같이 주석이 @Mapper 대신 @Repository.

 

PeopleService.java

package tgc.edu.wx.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tgc.edu.wx.entity.People;
import tgc.edu.wx.mapper.PeopleMapper;

@Service
public class PeopleService {

    @Autowired
    private PeopleMapper peopleDAO;
    
    public List<People> findAll() {
     return    peopleDAO.findAll();
    }
}

 

PeopleController.java

package tgc.edu.wx.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.wx.entity.People;
import tgc.edu.wx.service.PeopleService;

@Controller
@RequestMapping("/people")
public class PeopleController {
    
    @Autowired
    private PeopleService peopleService;
    
    
    @RequestMapping("/list")
    public String list(ModelMap map) {
         List<People> peoples = peopleService.findAll();
        map.put("peoples", peoples);
        return "peopleList";
    }
}

 

数据库:

 

 

 

4.依照 application.properties 中编写的地址在对应目录下创建xml格式的mybatis映射文件。

<?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="tgc.edu.wx.mapper.PeopleMapper">

<select id="findAll" resultType="tgc.edu.wx.entity.People">
SELECT * FROM people
</select>
</mapper>

其中<select>标签内的 id 对应的是dao层内的方法名,resultType对应的是查询返回结果集的类型,select内填写对应方法的语句即可。

 

5.最后,在启动类里加上注解用于给出需要扫描的mapper文件路径

package tgc.edu.wx;

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

@SpringBootApplication
@MapperScan("tgc.edu.wx.mapper")
public class MybatisDemoApplication {

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

}

 

启动项目测试一下:

 

以上。

 

추천

출처www.cnblogs.com/ElPsyCongroowx/p/11247628.html