詳細に春ブーツ統合MyBatisの設定

まず、あなたは春のブートプロジェクトを持っている必要があります。

 

通常、共通リポジトリのパッケージには、MyBatisの年でマッパーを交換されている開発。

 

 

構成:

1.依存性が導入さ:

< 依存性> 
  < のgroupId > org.mybatis.spring.boot </ のgroupId > 
   < たartifactId > MyBatisのスプリングブートスタータ</ たartifactId > 
   < バージョン> 1.1.1 </ バージョン> 
</ 依存>

 

2.編集application.properties

spring.datasource.url = JDBCます。mysql:// localhostを:?3306 /テストuseSslオプション=真&serverTimezone = GMT 
spring.datasource.username = ルート
spring.datasource.password = ルート
spring.datasource.driver - クラス -name = com.mysql .jdbc.Driver 

spring.servlet.multipart.max -requestサイズ= 2050メガバイト
spring.servlet.multipart.max -fileサイズ= 2048メガバイトの

spring.jpa.hibernate.use - 新しい -id-発電マッピング= falseの
はserver.port = 8080 
server.servlet.context -path = / mybatisDemo 
spring.jmx.enabled = 

mybatis.type -aliases- パッケージを = tgc.edu.wx.entity 
mybatis.mapperLocations =クラスパス:マッピング/ * .xmlファイル

ここでは、第二は、アドレスマッピングファイルで、2つの構成MyBatisの、パッケージの1回のスキャンに注意しなければなりません。

 

以下の図が完了した後、所望のウェブ・アイテム・クラス、エンティティクラスとデータベース内の対応するテーブルの確立を確立する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