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

SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問應(yīng)用的實(shí)例代碼

 更新時(shí)間:2017年05月21日 19:24:45   作者:說話的方式簡(jiǎn)單點(diǎn)丶  
這篇文章主要介紹了SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問應(yīng)用的實(shí)例代碼,需要的朋友可以參考下

因?yàn)閷?shí)習(xí)用的是MyBatis框架,所以寫一篇關(guān)于SpringBoot整合MyBatis框架的總結(jié)。

一,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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging> //這里設(shè)置為jar,因?yàn)槲覀儠?huì)使用jar包部署運(yùn)行
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId> //Mybatis的jar包
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId> //json數(shù)據(jù)格式和對(duì)象的轉(zhuǎn)換jar包
      <version>1.9.8</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId> //內(nèi)嵌數(shù)據(jù)庫
      <artifactId>h2</artifactId> 
      <version>1.3.156</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId> //lombok插件,方便model對(duì)象的處理
      <version>1.16.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId> //mysql驅(qū)動(dòng)
      <version>5.1.18</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>example</finalName> //打包后的jar包名稱
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId> //必須要的SpringBoot繼承的maven插件,缺少了無法打包jar。
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId> //因?yàn)閖ar包中可能存在很多其他的配置資源,例如mapper文件所以打包為jar包需要將其加入,所以需要此資源打包插件
        <version>2.5</version>
        <executions>
          <execution>
            <id>copy-xmls</id>
            <phase>process-sources</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/classes</outputDirectory>
              <resources>
                <resource>
                  <directory>${basedir}/src/main/java</directory>
                  <includes>
                    <include>**/*.xml</include>
                  </includes>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    <resources> //打包包含相應(yīng)的資源文件
      <resource>
        <directory>src/main/resources</directory> 
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
          <include>**/*.tld</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
          <include>**/*.tld</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>
  <repositories>//設(shè)置倉庫
    <repository>
      <id>spring-milestone</id>
      <url>http://repo.spring.io/libs-release</url>
    </repository>
  </repositories>
</project>

好了簡(jiǎn)單的SpringBoot整合Mybatis框架的基礎(chǔ)環(huán)境已經(jīng)搭建完成了,一個(gè)Pom文件搞定,接下來我們配置我們的配置文件。

二,配置文件

我們寫在resources目錄下的application.properties文件中。

spring.datasource.url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫名稱?useUnicode=true&characterEncoding=UTF8
spring.datasource.username=用戶名
spring.datasource.password=用戶密碼
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath*:/mapper/*Mapper.xml //mapper文件的路徑
mybatis.type-aliases-package=map.model //mapper文件中的前綴
server.port=監(jiān)聽端口號(hào),不設(shè)置默認(rèn)8080

ok,現(xiàn)在環(huán)境已經(jīng)徹底搭建完成我們可以編寫自己的代碼了。

三,編寫代碼

Model對(duì)象

@Data//@Data lombok插件的注解自動(dòng)添加get set方法
public class ExampleModel {
  private Long id;
  private String name;
}
//一個(gè)簡(jiǎn)單的model對(duì)象

Dao層

這里需要注意的是,推薦公司使用的一種做法,因?yàn)楹芏嗟腄ao對(duì)象都是簡(jiǎn)單的增刪改查功能,所以我們抽象出一個(gè)最基本的父類,這個(gè)父類實(shí)現(xiàn)最基本的增刪改查功能,每個(gè)新的Dao對(duì)象可以繼承這個(gè)類,然后自定義實(shí)現(xiàn)特殊的數(shù)據(jù)庫訪問功能,我們可以把這個(gè)基本的父類成為MyBatisHelper并用上泛型,具體實(shí)現(xiàn)如下:

@Data
public class MybatisHelper<T> {
  @Autowired
  private SqlSession sqlSession; //這里自動(dòng)注入mybatis的SqlSession
  private String nameSpace;
  public MybatisHelper(String nameSpace) {
    this.nameSpace = nameSpace;
  }
  public String getSqlName(String sqlName) {
    return nameSpace +"."+ sqlName;
  }
  public Integer create(String name, T obj) {
    return sqlSession.insert(getSqlName(name), obj);
  }
  public Boolean update(String name, T obj) {
    return Boolean.valueOf(sqlSession.update(getSqlName(name), obj) > 0);
  }
  public T findById(String name, Long id) {
    return sqlSession.selectOne(getSqlName(name), id);
  }
  public Boolean delete(String name, Long id) {
    return Boolean.valueOf(sqlSession.delete(getSqlName(name), id) > 0);
  }
  public List<T> findAll(String name){return sqlSession.selectList(getSqlName(name));}
}

需要說明的是因?yàn)閟qlSession的執(zhí)行回去尋找相應(yīng)的mapper文件,所以namespace+方法名稱很重要,這個(gè)一定要注意不要弄錯(cuò)了,弄錯(cuò)了就會(huì)無法正確調(diào)用。

然后我們的Dao層實(shí)現(xiàn)繼承此類

@Component
public class ExampleModelDao extends MybatisHelper<ExampleModel>{
  public ExampleModelDao() {
    super("example.dao.");
  }
//todo 自定義操作
public Integer findDataCounts(){
  return getSqlSession().selectOne(getSqlName("findDataCounts"));//他會(huì)尋找example.dao.findDataCounts對(duì)應(yīng)的方法執(zhí)行
}
}

這樣是不是很簡(jiǎn)單,也能大量復(fù)用很省事,關(guān)于service層我就不寫了很簡(jiǎn)單。

四,mapper文件

<?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="example.dao">//這里很重要就是前綴
  <resultMap id="ExampleModelMap" type="ExampleMode">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
  </resultMap> //自定義resultMap對(duì)象,利于對(duì)象的操作
  <sql id="tb"> //數(shù)據(jù)表標(biāo)簽
    example_data
  </sql>
  <sql id="value_exclude_id"> //除了主鍵以為的字段集合標(biāo)簽
    name
  </sql>
  <sql id="vls"> //插入屬性的字段集合標(biāo)簽
    id,name
  </sql>
  <sql id="insert_value">//插入輸入進(jìn)來的字段值標(biāo)簽
    #{name}
  </sql>
  <insert id="create" parameterType="ExampleModel">
    INSERT INTO <include refid="tb"/> (<include refid="value_exclude_id"/>) VALUES (<include refid="insert_value"/>)
  </insert>//一看就明白了創(chuàng)建一個(gè)對(duì)象
  <select id="findById" parameterType="long" resultMap="ExampleModelMap">//返回我們定義的resultMap
    SELECT <include refid="vls"/> FROM <include refid="tb"/> WHERE id = #{id}
  </select>
  <select id="findAll" resultMap="ExampleModelMap">
    SELECT <include refid="vls"/> FROM <include refid="tb"/>
  </select>
  <select id="findDataCounts" resultType="int">
    SELECT count(1) FROM <include refid="tb"/>
  </select>//自定義的操作
</mapper>

ok,對(duì)應(yīng)的mapper文件已經(jīng)有了,我們就可以調(diào)用了,調(diào)用很簡(jiǎn)單一般寫在service層中調(diào)用,下面我們?nèi)ゾ帉憣?duì)應(yīng)的controller。

五,控制器編寫

推薦使用restful風(fēng)格,因此我們控制器編寫代碼如下:

@RestController
@CrossOrigin //這個(gè)是ajax跨域請(qǐng)求允許的注解,不用可以去掉
public class DigMapDataController {
  @Autowired
  private ExampleService exampleService;//service對(duì)象
  @RequestMapping(value = "/create", method = RequestMethod.POST)
  public String create(@requestBody ExampleModel exampleModel) {
    return String.valueOf(exampleService.create(exampleModel));
}
//@requestBody注解會(huì)接受前端的JSON數(shù)據(jù)并配合jackson自動(dòng)轉(zhuǎn)換為相應(yīng)的對(duì)象
  @RequestMapping(value = "/find/count",method = RequestMethod.GET)
  public Integer findCounts() {
    return exampleService.findDataCounts();
  }
}

一個(gè)簡(jiǎn)單的控制器就編寫完成了,這個(gè)時(shí)候我們可以啟動(dòng)應(yīng)用進(jìn)行數(shù)據(jù)訪問了,是不是很簡(jiǎn)單。

六,應(yīng)用的部署

直接在終端中使用命令,將應(yīng)用打包為jar文件

1.maven  [clean]  package ;打包后的文件在target目錄下

2.java -jar example.jar ; 運(yùn)行我們的jar包程序

ok 大功告成!

以上所述是小編給大家介紹的SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問應(yīng)用的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言!

相關(guān)文章

  • SpringBoot使用redis實(shí)現(xiàn)session共享功能

    SpringBoot使用redis實(shí)現(xiàn)session共享功能

    這篇文章主要介紹了pringboot項(xiàng)目使用redis實(shí)現(xiàn)session共享,文中通過代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-05-05
  • MyBatis中多對(duì)多關(guān)系的映射和查詢

    MyBatis中多對(duì)多關(guān)系的映射和查詢

    本文主要介紹了MyBatis中多對(duì)多關(guān)系的映射和查詢的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • 詳解Java設(shè)計(jì)模式之橋接模式

    詳解Java設(shè)計(jì)模式之橋接模式

    橋接,顧名思義,就是用來連接兩個(gè)部分,使得兩個(gè)部分可以互相通訊。橋接模式將系統(tǒng)的抽象部分與實(shí)現(xiàn)部分分離解耦,使他們可以獨(dú)立的變化。本文通過示例詳細(xì)介紹了橋接模式的原理與使用,需要的可以參考一下
    2022-10-10
  • Java基礎(chǔ)教程之實(shí)現(xiàn)接口

    Java基礎(chǔ)教程之實(shí)現(xiàn)接口

    這篇文章主要介紹了Java基礎(chǔ)教程之實(shí)現(xiàn)接口,也可以說是實(shí)施接口,因?yàn)榻涌谥皇嵌x,最終要實(shí)現(xiàn)它,本文就專門講解接口的實(shí)現(xiàn),需要的朋友可以參考下
    2014-08-08
  • Java工作隊(duì)列代碼詳解

    Java工作隊(duì)列代碼詳解

    這篇文章主要介紹了Java工作隊(duì)列代碼詳解,涉及Round-robin 轉(zhuǎn)發(fā),消息應(yīng)答(messageacknowledgments),消息持久化(Messagedurability)等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • 為Java應(yīng)用創(chuàng)建Docker鏡像的3種方式總結(jié)

    為Java應(yīng)用創(chuàng)建Docker鏡像的3種方式總結(jié)

    Docker的使用可以將應(yīng)用程序做成鏡像,這樣可以將鏡像發(fā)布到私有或者公有倉庫中,在其他主機(jī)上也可以pull鏡像,并且運(yùn)行容器,運(yùn)行程,下面這篇文章主要給大家總結(jié)介紹了關(guān)于為Java應(yīng)用創(chuàng)建Docker鏡像的3種方式,需要的朋友可以參考下
    2023-06-06
  • java實(shí)現(xiàn)的二級(jí)聯(lián)動(dòng)菜單效果

    java實(shí)現(xiàn)的二級(jí)聯(lián)動(dòng)菜單效果

    這篇文章主要介紹了java實(shí)現(xiàn)的二級(jí)聯(lián)動(dòng)菜單效果,結(jié)合實(shí)例形式分析了java前臺(tái)頁面布局及與后臺(tái)交互構(gòu)造聯(lián)動(dòng)菜單的相關(guān)技巧,需要的朋友可以參考下
    2016-08-08
  • 如何將Java與C#時(shí)間進(jìn)行互相轉(zhuǎn)換

    如何將Java與C#時(shí)間進(jìn)行互相轉(zhuǎn)換

    這篇文章主要介紹了Java與C#時(shí)間互轉(zhuǎn)的方法以及JAVA日期、C#日期計(jì)算說明,需要的朋友可以參考下
    2022-11-11
  • Spring中的FactoryBean實(shí)現(xiàn)原理詳解

    Spring中的FactoryBean實(shí)現(xiàn)原理詳解

    這篇文章主要介紹了Spring中的FactoryBean實(shí)現(xiàn)原理詳解,spring中有兩種類型的Bean,一種是普通的JavaBean,另一種就是工廠Bean(FactoryBean),這兩種Bean都受Spring的IoC容器管理,但它們之間卻有一些區(qū)別,需要的朋友可以參考下
    2024-02-02
  • java中的SpringBoot框架

    java中的SpringBoot框架

    這篇文章主要介紹了java學(xué)習(xí)之SpringBoot框架,文章基于Java的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-04-04

最新評(píng)論

封丘县| 文成县| 永靖县| 雷波县| 咸宁市| 海宁市| 监利县| 周口市| 宝应县| 太仆寺旗| 通江县| 湛江市| 蓝山县| 明星| 石家庄市| 平塘县| 阿拉善右旗| 甘南县| 邵阳县| 府谷县| 灯塔市| 郸城县| 莱芜市| 金阳县| 乌什县| 柯坪县| 泸定县| 怀集县| 汤阴县| 婺源县| 谷城县| 延吉市| 松江区| 承德县| 天津市| 民和| 青河县| 金川县| 南丰县| 修水县| 灯塔市|