最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring boot2結(jié)合mybatis增刪改查的實(shí)現(xiàn)

 更新時(shí)間:2019年09月11日 08:24:34   作者:軟件老王  
這篇文章主要給大家介紹了關(guān)于spring boot2結(jié)合mybatis增刪改查的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot2具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

1. 場(chǎng)景描述

本節(jié)結(jié)合springboot2、springmvc、mybatis、swagger2等,搭建一個(gè)完整的增刪改查項(xiàng)目,希望通過這個(gè)基礎(chǔ)項(xiàng)目,能幫忙朋友快速上手springboot2項(xiàng)目。

2. 解決方案

2.1新建springboot項(xiàng)目

使用idea新建springboot項(xiàng)目(springboot項(xiàng)目快速搭建

(1)new project

(2)gav設(shè)置

2.2 項(xiàng)目整體圖及說明2.2.1 整體圖

2.2.2 說明

項(xiàng)目包含4大內(nèi)容

(1)pom.xml

maven項(xiàng)目必備,用于定義項(xiàng)目、獲取jar包、打包等。

(2)項(xiàng)目配置文件

有兩個(gè),一個(gè)是項(xiàng)目內(nèi)配置文件;一個(gè)是用于mybatis-generate生成相關(guān)數(shù)據(jù)庫操作文件。

(3)spcrudapplication

項(xiàng)目啟動(dòng)類,springboot項(xiàng)目必備。

(4)springmvc對(duì)應(yīng)類。

包含controller、service、db等相關(guān)類。

2.3 詳細(xì)說明

2.3.1 pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.7.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.laowang</groupId>
 <artifactId>spcrud</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>spcrud</name>
 <description>Demo project for Spring Boot</description>

 <properties>
  <java.version>1.8</java.version>
 </properties>
 <dependencies>
  <!--1.web啟動(dòng)包 軟件老王-->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>

  <!--2. 數(shù)據(jù)庫 軟件老王-->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>8.0.15</version>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
   <version>2.1.5.RELEASE</version>
  </dependency>

  <!--3. swagger 軟件老王-->
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.7.0</version>
  </dependency>
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.7.0</version>
  </dependency>
  <!--4.mybatis 軟件老王-->
  <!--mybatis-->
  <dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.1.0</version>
 </dependency>

 </dependencies>

  <!--5.打包 軟件老王-->
 <build>
  <resources>
   <resource>
    <directory>src/main/resources/</directory>
   </resource>
   <resource>
    <directory>src/main/java</directory>
    <includes>
     <include>**/*.xml</include>
    </includes>
   </resource>
  </resources>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>

   <plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.5</version>
    <configuration>
     <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
     <verbose>true</verbose>
     <overwrite>true</overwrite>
    </configuration>
   </plugin>
  </plugins>
 </build>

</project>

說明:

包含5塊內(nèi)容

(1)web啟動(dòng)包 ;

(2)數(shù)據(jù)庫 ;

(3)swagger;

(4)mybatis;

(5)打包;

2.3.2 資源文件

(1)application.properties

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ruanjianlaowang?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root

說明: 數(shù)據(jù)庫配置文件,連接、用戶名、密碼

(2)mybatis資源文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
 <classPathEntry
   location="E:\m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/>
 <context id="DB2Tables" targetRuntime="MyBatis3">
  <!--<plugin type="org.mybatis.generator.plugins.ExamplePagePlugin"/>-->
  <!--<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>-->
  <commentGenerator>
   <!-- 是否去除自動(dòng)生成的注釋 true:是 : false:否 -->
   <property name="suppressAllComments" value="true"/>
  </commentGenerator>

  <jdbcConnection driverClass="com.mysql.jdbc.Driver"
      connectionURL="jdbc:mysql://127.0.0.1:3306/ruanjianlaowang"
      userId="root"
      password="root">
  </jdbcConnection>

  <javaTypeResolver>
   <property name="forceBigDecimals" value="false"/>
  </javaTypeResolver>
  <javaModelGenerator targetPackage="com.laowang.spcrud.db.entity" targetProject="src/main/java">
   <property name="enableSubPackages" value="true"/>
   <property name="trimStrings" value="true"/>
  </javaModelGenerator>
  <sqlMapGenerator targetPackage="com.laowang.spcrud.db.mapper" targetProject="src/main/java">
   <property name="enableSubPackages" value="true"/>
  </sqlMapGenerator>
  <javaClientGenerator type="XMLMAPPER" targetPackage="com.laowang.spcrud.db.mapper" targetProject="src/main/java">
   <property name="enableSubPackages" value="true"/>
  </javaClientGenerator>


  <table tableName="t_laowang" domainObjectName="TLaowang" enableInsert="true"
    enableDeleteByPrimaryKey="true"
    enableSelectByPrimaryKey="true"
    enableUpdateByPrimaryKey="true"
    enableCountByExample="false"
    enableDeleteByExample="false"
    enableSelectByExample="false"
    enableUpdateByExample="false">
   <property name="useActualColumnNames" value="false"/>
   <generatedKey column="id" sqlStatement="MYSQL" identity="true"/>
  </table>


 </context>
</generatorConfiguration>

說明:

包含幾塊內(nèi)容:

(a)classPathEntry 標(biāo)簽定義的是mysql-connector的jar包地址

(b)jdbcConnection 數(shù)據(jù)庫連接信息

(c)javaModelGenerator、sqlMapGenerator、javaClientGenerator定義的是生成文件存放的地址;

(d)table具體執(zhí)行生成代碼的tabel,增加幾個(gè)標(biāo)簽,不生成example方法。

2.3.3 啟動(dòng)類

package com.laowang.spcrud;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@MapperScan("com.laowang.spcrud.db.mapper")
public class SpcrudApplication {

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

}

說明:

@SpringBootApplication所有springboot項(xiàng)目啟動(dòng)必備

@EnableSwagger2 啟動(dòng)swagger

@MapperScan加載mpper文件。

2.3.4 springmvc類

(1)TestController

package com.laowang.spcrud;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@MapperScan("com.laowang.spcrud.db.mapper")
public class SpcrudApplication {

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

}

ctroller類包含增刪改查4個(gè)方法,使用了rest請(qǐng)求的方式。

(2)TestService

package com.laowang.spcrud.service;

import com.laowang.spcrud.db.entity.TLaowang;
import com.laowang.spcrud.db.mapper.TLaowangMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {
 @Autowired
 private TLaowangMapper tLaowangMapper;


 /**
  * 增加
  * @auther: 軟件老王
  */
 public void insertRecord(TLaowang tLaowang) {
  tLaowangMapper.insert(tLaowang);
 }

 /**
  * 刪除
  * @auther: 軟件老王
  */
 public void deleteByPrimaryKey(int id) {
  tLaowangMapper.deleteByPrimaryKey(id);
 }
 /**
  * 更新
  * @auther: 軟件老王
  */
 public void updateByPrimaryKeySelective(TLaowang tLaowang) {
  tLaowangMapper.updateByPrimaryKeySelective(tLaowang);
 }

 /**
  * 查詢
  * @auther: 軟件老王
  */
 public TLaowang selectByPrimaryKey(int id) {
  return tLaowangMapper.selectByPrimaryKey(id);
 }
}

TestService類,增刪改查的服務(wù)類。

(3)實(shí)體類TLaowang

package com.laowang.spcrud.db.entity;

public class TLaowang {
 private Integer id;

 private String name;

 private String password;

 public Integer getId() {
  return id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name == null ? null : name.trim();
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password == null ? null : password.trim();
 }
}

操作實(shí)體類,包含三個(gè)字段:id、name、password

(4)mpper接口類TLaowangMapper

package com.laowang.spcrud.db.mapper;

import com.laowang.spcrud.db.entity.TLaowang;

public interface TLaowangMapper {
 int deleteByPrimaryKey(Integer id);

 int insert(TLaowang record);

 int insertSelective(TLaowang record);

 TLaowang selectByPrimaryKey(Integer id);

 int updateByPrimaryKeySelective(TLaowang record);

 int updateByPrimaryKey(TLaowang record);
}

(5)mapper接口xml

<?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="com.laowang.spcrud.db.mapper.TLaowangMapper">
 <resultMap id="BaseResultMap" type="com.laowang.spcrud.db.entity.TLaowang">
 <id column="id" jdbcType="INTEGER" property="id" />
 <result column="name" jdbcType="VARCHAR" property="name" />
 <result column="password" jdbcType="VARCHAR" property="password" />
 </resultMap>
 <sql id="Base_Column_List">
 id, name, password
 </sql>
 <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
 select 
 <include refid="Base_Column_List" />
 from t_laowang
 where id = #{id,jdbcType=INTEGER}
 </select>
 <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
 delete from t_laowang
 where id = #{id,jdbcType=INTEGER}
 </delete>
 <insert id="insert" parameterType="com.laowang.spcrud.db.entity.TLaowang">
 <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
  SELECT LAST_INSERT_ID()
 </selectKey>
 insert into t_laowang (name, password)
 values (#{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
 </insert>
 <insert id="insertSelective" parameterType="com.laowang.spcrud.db.entity.TLaowang">
 <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
  SELECT LAST_INSERT_ID()
 </selectKey>
 insert into t_laowang
 <trim prefix="(" suffix=")" suffixOverrides=",">
  <if test="name != null">
  name,
  </if>
  <if test="password != null">
  password,
  </if>
 </trim>
 <trim prefix="values (" suffix=")" suffixOverrides=",">
  <if test="name != null">
  #{name,jdbcType=VARCHAR},
  </if>
  <if test="password != null">
  #{password,jdbcType=VARCHAR},
  </if>
 </trim>
 </insert>
 <update id="updateByPrimaryKeySelective" parameterType="com.laowang.spcrud.db.entity.TLaowang">
 update t_laowang
 <set>
  <if test="name != null">
  name = #{name,jdbcType=VARCHAR},
  </if>
  <if test="password != null">
  password = #{password,jdbcType=VARCHAR},
  </if>
 </set>
 where id = #{id,jdbcType=INTEGER}
 </update>
 <update id="updateByPrimaryKey" parameterType="com.laowang.spcrud.db.entity.TLaowang">
 update t_laowang
 set name = #{name,jdbcType=VARCHAR},
  password = #{password,jdbcType=VARCHAR}
 where id = #{id,jdbcType=INTEGER}
 </update>
</mapper>

4與5在一起,這里使用了mybatis自動(dòng)生成的增刪改查方法,未做擴(kuò)展,真實(shí)項(xiàng)目中除了這幾個(gè)外,肯定還會(huì)做些擴(kuò)展,比如根據(jù)name查詢等。

2.4 數(shù)據(jù)庫建表語句

CREATE TABLE `t_laowang` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(50) DEFAULT NULL,
 `password` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

2.5 swagger效果

http://localhost:8080/swagger-ui.html

完整的代碼,完整的注釋,希望對(duì)你有幫助。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Spring Security基于json登錄實(shí)現(xiàn)過程詳解

    Spring Security基于json登錄實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了Spring Security基于json登錄實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Java幸運(yùn)28系統(tǒng)搭建數(shù)組的使用實(shí)例詳解

    Java幸運(yùn)28系統(tǒng)搭建數(shù)組的使用實(shí)例詳解

    在本篇文章里小編給大家整理了關(guān)于Java幸運(yùn)28系統(tǒng)搭建數(shù)組的使用實(shí)例內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。
    2019-09-09
  • Spring?Boot自定義監(jiān)控指標(biāo)的詳細(xì)過程

    Spring?Boot自定義監(jiān)控指標(biāo)的詳細(xì)過程

    這篇文章主要介紹了Spring?Boot如何自定義監(jiān)控指標(biāo)?,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • springboot中restful風(fēng)格請(qǐng)求的使用方法示例

    springboot中restful風(fēng)格請(qǐng)求的使用方法示例

    RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個(gè)資源定位(url)及資源操作的風(fēng)格,下面這篇文章主要給大家介紹了關(guān)于springboot中restful風(fēng)格請(qǐng)求的使用方法,需要的朋友可以參考下
    2023-02-02
  • 深入淺析jcmd:JDK14中的調(diào)試神器

    深入淺析jcmd:JDK14中的調(diào)試神器

    這篇文章主要介紹了jcmd:JDK14中的調(diào)試神器,本文給大家提到了jcmd的語法,通過實(shí)例列舉的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 淺談Java中的參數(shù)傳遞問題

    淺談Java中的參數(shù)傳遞問題

    這篇文章主要介紹了Java中的參數(shù)傳遞問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • springMVC攔截器HandlerInterceptor用法代碼示例

    springMVC攔截器HandlerInterceptor用法代碼示例

    這篇文章主要介紹了springMVC攔截器HandlerInterceptor用法代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • 簡(jiǎn)單談?wù)刯ava自定義注解

    簡(jiǎn)單談?wù)刯ava自定義注解

    下面小編就為大家?guī)硪黄?jiǎn)單談?wù)刯ava自定義注解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • Flowable?ReceiveTask使用場(chǎng)景分析

    Flowable?ReceiveTask使用場(chǎng)景分析

    這篇文章主要為大家介紹了Flowable?ReceiveTask使用場(chǎng)景分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Spring?Boot?Yaml配置高級(jí)用法

    Spring?Boot?Yaml配置高級(jí)用法

    這篇文章主要介紹了Spring?Boot?Yaml配置高級(jí)用法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12

最新評(píng)論

连山| 开化县| 林州市| 钟山县| 达州市| 渝中区| 班玛县| 岳普湖县| 东乌| 新宁县| 靖安县| 灵璧县| 嘉义县| 东兴市| 武汉市| 赞皇县| 师宗县| 阿鲁科尔沁旗| 禹城市| 西乌珠穆沁旗| 金门县| 镇安县| 沾益县| 霍林郭勒市| 和田县| 安远县| 南川市| 敦煌市| 宜兰市| 旬邑县| 香河县| 芦溪县| 衡南县| 新兴县| 卓尼县| 徐汇区| 茌平县| 即墨市| 静安区| 新民市| 迁安市|