1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringBoot整合Mybatis圖文教程

        共 4085字,需瀏覽 9分鐘

         ·

        2020-08-20 05:17


        這里介紹兩種整合SpringBoot和Mybatis的模式,分別是“全注解版” 和 “注解xml合并版”。


        前期準(zhǔn)備


        開發(fā)環(huán)境


        • 開發(fā)工具:

          IDEA

        • JDK:

          1.8

        • 技術(shù):

          SpringBoot、Maven、Mybatis


        創(chuàng)建項(xiàng)目







        項(xiàng)目結(jié)構(gòu)



        Maven依賴


        xml version="1.0"?encoding="UTF-8"?>
        <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>com.examplegroupId>
        ??<artifactId>demoartifactId>
        ??<version>0.0.1-SNAPSHOTversion>
        ??<packaging>jarpackaging>

        ??<name>demoname>
        ??<description>Demo project for Spring Bootdescription>

        ??<parent>
        ????<groupId>org.springframework.bootgroupId>
        ????<artifactId>spring-boot-starter-parentartifactId>
        ????<version>2.0.0.RELEASEversion>
        ????<relativePath/>?
        ??parent>

        ??<properties>
        ????<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        ????<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        ????<java.version>1.8java.version>
        ??properties>

        ??<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>
        ??????<scope>runtimescope>
        ????dependency>
        ????<dependency>
        ??????<groupId>org.springframework.bootgroupId>
        ??????<artifactId>spring-boot-starter-testartifactId>
        ??????<scope>testscope>
        ????dependency>
        ??dependencies>

        ??<build>
        ????<plugins>
        ??????<plugin>
        ????????<groupId>org.springframework.bootgroupId>
        ????????<artifactId>spring-boot-maven-pluginartifactId>
        ??????plugin>
        ????plugins>
        ??build>
        project>


        全注解版


        SpringBoot配置文件


        這里使用yml格式的配置文件,將application.properties改名為application.yml。


        #配置數(shù)據(jù)源
        spring:
        ??datasource:
        ?????url:?jdbc:mysql://127.0.0.1:3306/dianping?useUnicode=true&characterEncoding=utf8
        ?????username:?root
        ?????password:?123
        ?????driver-class-name: com.mysql.jdbc.Driver


        SpringBoot會(huì)自動(dòng)加載application.yml相關(guān)配置,數(shù)據(jù)源就會(huì)自動(dòng)注入到sqlSessionFactory中,sqlSessionFactory會(huì)自動(dòng)注入到Mapper中。


        實(shí)體類


        public?class?Happiness?{
        ????private?Long id;
        ????private?String city;
        ????private?Integer num;

        ??//getters、setters、toString
        }


        映射類


        @Mapper
        public interface HappinessDao {
        ????@Select("SELECT * FROM happiness WHERE city = #{city}")
        ????Happiness findHappinessByCity(@Param("city") String city);

        ????@Insert("INSERT INTO happiness(city, num) VALUES(#{city}, #{num})")
        ????int insertHappiness(@Param("city") String city, @Param("num") Integer num);
        }


        Service類


        事務(wù)管理只需要在方法上加個(gè)注解:@Transactional


        @Service
        public?class?HappinessService?{
        ????@Autowired
        ????private?HappinessDao happinessDao;

        ????public?Happiness selectService(String city){
        ????????return?happinessDao.findHappinessByCity(city);
        ????}

        ????@Transactional
        ????public?void?insertService(){
        ????????happinessDao.insertHappiness("西安", 9421);
        ????????int?a = 1?/ 0; //模擬故障
        ????????happinessDao.insertHappiness("長(zhǎng)安", 1294);
        ????}
        }


        Controller類


        @RestController
        @RequestMapping("/demo")
        public class HappinessController {
        ????@Autowired
        ????private HappinessService happinessService;

        ????@RequestMapping("/query")
        ????public Happiness testQuery(){
        ????????return?happinessService.selectService("北京");
        ????}

        ????@RequestMapping("/insert")
        ????public?Happiness?testInsert(){
        ????????happinessService.insertService();
        ????????return?happinessService.selectService("西安");
        ????}
        }


        測(cè)試


        http://localhost:8080/demo/query
        http://localhost:8080/demo/insert


        注解xml合并版


        項(xiàng)目結(jié)構(gòu)



        SpringBoot配置文件


        #配置數(shù)據(jù)源
        spring:
        ??datasource:
        ?????url:?jdbc:mysql://127.0.0.1:3306/dianping
        ?????username: root
        ?????password: 123
        ?????driver-class-name: com.mysql.jdbc.Driver

        #指定mybatis映射文件的地址
        mybatis:
        ??mapper-locations:?classpath:mapper/*.xml


        映射類


        @Mapper
        public?interface?HappinessDao {
        ????Happiness findHappinessByCity(String?city);
        ????int insertHappiness(HashMap<String, Object> map);
        }


        映射文件


        <mapper?namespace="com.example.demo.dao.HappinessDao">
        ????<select?id="findHappinessByCity"?parameterType="String"?resultType="com.example.demo.domain.Happiness">
        ????????SELECT * FROM happiness WHERE city = #{city}
        ????select>

        ????<insert?id="insertHappiness"?parameterType="HashMap"?useGeneratedKeys="true"?keyProperty="id">
        ????????INSERT INTO happiness(city, num) VALUES(#{city}, #{num})
        ????insert>
        mapper>

        Service類


        事務(wù)管理只需要在方法上加個(gè)注解:@Transactional


        @Service
        public?class?HappinessService?{
        ????@Autowired
        ????private?HappinessDao happinessDao;

        ????public?Happiness selectService(String city){
        ????????return?happinessDao.findHappinessByCity(city);
        ????}

        ????@Transactional
        ????public?void?insertService()
        {
        ????????HashMap map?= new?HashMap();
        ????????map.put("city", "西安");
        ????????map.put("num", 9421);

        ????????happinessDao.insertHappiness(map);
        ????????int?a = 1?/ 0; //模擬故障
        ????????happinessDao.insertHappiness(map);
        ????}
        }


        Controller類


        @RestController
        @RequestMapping("/demo")
        public class HappinessController {
        ????@Autowired
        ????private HappinessService happinessService;

        ????@RequestMapping("/query")
        ????public Happiness testQuery(){
        ????????return?happinessService.selectService("北京");
        ????}

        ????@RequestMapping("/insert")
        ????public?Happiness?testInsert(){
        ????????happinessService.insertService();
        ????????return?happinessService.selectService("西安");
        ????}
        }


        測(cè)試


        http://localhost:8080/demo/query
        http://localhost:8080/demo/insert



        原文鏈接:csdn.net/litianxiang_kaola/article/details/79481422



        瀏覽 59
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产菊爆A片免费 | 在线无码成人电影 | 欧美特黄视频 | 亚洲激情无码中文字幕 | 日本少妇AA一级特黄大片 | 三级黄色软件下载 | 欧美成人电影一区二区 | 亚洲成人资源 | 中文字幕在线观看网址 | 青娱在线视频 |