<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          MyBatis Plus入門使用

          共 11151字,需瀏覽 23分鐘

           ·

          2021-03-26 08:38

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”

          優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

           

          Mybatis在持久層框架中還是比較火的,可以通過解析xml文件中的SQL語(yǔ)句,從而操作數(shù)據(jù)庫(kù)。但由于需要寫SQL語(yǔ)句,所以會(huì)產(chǎn)生大量的xml文件,而且還存在著大量的簡(jiǎn)單的CRUD語(yǔ)句,這無疑大大降低了程序員的開發(fā)效率,MyBatis-Plus正好彌補(bǔ)了這些短板。


          MyBatis Plus 是國(guó)內(nèi)人員開發(fā)的 MyBatis 增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生。MyBatis Plus 的核心功能有:支持通用的 CRUD、代碼生成器與條件構(gòu)造器。


          MyBatis-Plus 入門

          本示例程序是基于JDK 1.8 + MySql 8.0 + Maven 3.6.1 + IDEA2020的基礎(chǔ)上進(jìn)行開發(fā)的


          1.創(chuàng)建Maven工程,引入核心依賴

          <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
          <!--mybatis-plus依賴-->
                  <dependency>
                      <groupId>com.baomidou</groupId>
                      <artifactId>mybatis-plus</artifactId>
                      <version>2.3</version>
                  </dependency>
                  <dependency>
                      <groupId>mysql</groupId>
                      <artifactId>mysql-connector-java</artifactId>
                      <version>5.1.47</version>
                  </dependency>
                  <!--lombok依賴-->
                 <dependency>
                      <groupId>org.projectlombok</groupId>
                      <artifactId>lombok</artifactId>
                      <version>1.18.16</version>
                  </dependency>
                  <!-- https://mvnrepository.com/artifact/log4j/log4j -->
                  <dependency>
                      <groupId>log4j</groupId>
                      <artifactId>log4j</artifactId>
                      <version>1.2.17</version>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework</groupId>
                      <artifactId>spring-orm</artifactId>
                      <version>5.0.5.RELEASE</version>
                  </dependency>
                  <dependency>
                      <groupId>org.springframework</groupId>
                      <artifactId>spring-context</artifactId>
                      <version>5.0.5.RELEASE</version>
                  </dependency>


          2.配置數(shù)據(jù)源,整合Spring與mybatis-plus


          (1)數(shù)據(jù)源 jdbc.properties

          jdbc.driver=com.mysql.jdbc.Driver
          jdbc.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
          jdbc.username=root
          jdbc.password=root


          (2)MyBatis 全局配置文件 mybatis-config.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE configuration
                  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
                  "http://mybatis.org/dtd/mybatis-3-config.dtd">
          <configuration>
          </configuration>


          (3) Spring 配置文件 spring-dao.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:p="http://www.springframework.org/schema/p"
                 xmlns:aop="http://www.springframework.org/schema/aop"
                 xmlns:context="http://www.springframework.org/schema/context"
                 xmlns:jee="http://www.springframework.org/schema/jee"
                 xmlns:tx="http://www.springframework.org/schema/tx"
                 xsi:schemaLocation="
                  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
                  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
          >

              <!-- 配置整合mybatis-plus過程 -->
              <!-- 1、配置數(shù)據(jù)庫(kù)相關(guān)參數(shù)properties -->
              <context:property-placeholder location="classpath:jdbc.properties" />
              <!-- 2、配置數(shù)據(jù)庫(kù)連接池 -->
              <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
                  <property name="driverClassName" value="${jdbc.driver}"/>
                  <property name="url" value="${jdbc.url}"/>
                  <property name="username" value="${jdbc.username}"/>
                  <property name="password" value="${jdbc.password}"/>
              </bean>

              <!-- mybatis的sqlsessionFactorybean:org.mybatis.spring.SqlSessionFactoryBean-->
              <!-- 3、配置mybatis-plus的sqlSessionFactory -->
              <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
                  <property name="dataSource" ref="dataSource" />
                  <property name="configLocation" value="classpath:mybatis-config.xml"/>
                  <property name="typeAliasesPackage" value="entity"/>
              </bean>

              <!-- 4、DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類 -->
              <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                  <property name="basePackage" value="dao" />
                  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
              </bean>
          </beans>


          3.編寫實(shí)體和接口

          (1)實(shí)體類 entity.Student

          package entity;

          import com.baomidou.mybatisplus.annotations.TableField;
          import com.baomidou.mybatisplus.annotations.TableId;
          import com.baomidou.mybatisplus.annotations.TableName;
          import com.baomidou.mybatisplus.enums.IdType;
          import lombok.Data;

          @Data
          @TableName("t_student")//與表名對(duì)應(yīng)
          public class Student {

              //value與數(shù)據(jù)庫(kù)主鍵列名一致,若實(shí)體類屬性名與表主鍵列名一致可省略value
              @TableId(value = "id",type = IdType.AUTO)//指定自增策略
              private Integer id;
              //若沒有開啟駝峰命名,或者表中列名不符合駝峰規(guī)則,可通過該注解指定數(shù)據(jù)庫(kù)表中的列名,exist標(biāo)明數(shù)據(jù)表中有沒有對(duì)應(yīng)列
          //    @TableField(value = "sno",exist = true)
              private String sno;
          //    @TableField(value = "sname",exist = true)
              private String sname;
          //    @TableField(value = "sex",exist = true)
              private String sex;

          }


          (2) 編寫接口 dao.StudentDao

          package dao;

          import com.baomidou.mybatisplus.mapper.BaseMapper;
          import entity.Student;

          //接口繼承BaseMapper<>
          public interface StudentDao extends BaseMapper<Student> {
          }



          4.測(cè)試

          (1)數(shù)據(jù)庫(kù)連接測(cè)試

          import dao.StudentDao;
          import entity.Student;
          import org.junit.Test;
          import org.junit.runner.RunWith;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.test.context.ContextConfiguration;
          import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

          import javax.sql.DataSource;
          import java.sql.SQLException;

          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration({"classpath:spring-dao.xml"})
          public class test {

              @Autowired
              private DataSource dataSource;

              @Autowired
              private StudentDao dao;

              @Test
              public void testDataSource() throws SQLException {
                  System.out.println(dataSource.getConnection());
              }

          (2)數(shù)據(jù)庫(kù)更新操作測(cè)試

          @Test
              public void testInsert(){
                  Student student = new Student();
                  student.setSno("123456");
                  student.setSex("男");
                  student.setSname("wyc");
                  dao.insert(student);
              }


          (3)數(shù)據(jù)庫(kù)查詢操作測(cè)試

          @Test
              public void testSelect(){
                  System.out.println(dao.selectById(97));
              }


          ————————————————

          版權(quán)聲明:本文為CSDN博主「Ego1stZz」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。

          原文鏈接:

          https://blog.csdn.net/qq_43739109/article/details/114991777




          粉絲福利:Java從入門到入土學(xué)習(xí)路線圖

          ??????

          ??長(zhǎng)按上方微信二維碼 2 秒


          感謝點(diǎn)贊支持下哈 

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

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  最近中文字幕mv第一季歌词免费 | 国产搞屄视频 | 一本大道AV伊人久久综合蜜芽 | 99热这里只有精品在线观看 | 日日夜夜AV |