亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Boot集成Mybatis的實(shí)例代碼(簡(jiǎn)潔版)

 更新時(shí)間:2018年02月05日 16:28:09   作者:Sam哥哥  
這篇文章主要介紹了Spring Boot集成Mybatis簡(jiǎn)潔版的教程,需要的朋友可以參考下

概述

現(xiàn)在互聯(lián)網(wǎng)應(yīng)用中,大部分還是使用Mybatis來(lái)操作數(shù)據(jù)庫(kù)的,本文介紹一下Spring Boot中如何集成Mybatis。

上篇介紹了Spring Boot 直接用jar運(yùn)行項(xiàng)目的方法,需要的朋友點(diǎn)擊查看。

創(chuàng)建Spring Boot工程

Spring Boot 開篇-創(chuàng)建和運(yùn)行 一文中有一個(gè)小節(jié)介紹了如何使用Spring Boot的組件來(lái)創(chuàng)建工程。如果要集成Mybatis,只需要把Mysql和Mybatis這兩個(gè)組件勾選一下即可。

 

當(dāng)然也可以不通過(guò)這種方式,直接在POM.xml文件中添加依賴也是可以的。我選擇的是直接在POM.xml文件中直接添加依賴這種方式。

dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.3.1</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.7</version>
</dependency>

數(shù)據(jù)源使用阿里的druid。完整的POM.xml文件內(nèi)容如下:

<?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.springboot</groupId>
 <artifactId>study</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>study</name>
 <description>Demo project for Spring Boot</description>
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.10.RELEASE</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <java.version>1.8</java.version>
 </properties>
 <dependencies>
 <dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.3.1</version>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
 </dependency>
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.7</version>
 </dependency>
 <dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.45</version>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
 </dependencies>
 <build>
 <plugins>
  <plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
 </plugins>
 </build>
</project>

創(chuàng)建table

CREATE TABLE `user` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `name` varchar(30) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='user信息';

創(chuàng)建entity

package com.springboot.entity;
public class User {
 private Long id;
 private String name;
 public Long getId() {
 return id;
 }
 public void setId(Long id) {
 this.id = id;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 @Override
 public String toString() {
 return "User{" +
  "id=" + id +
  ", name='" + name + '\'' +
  '}';
 }
}

創(chuàng)建Mybatis映射文件和repo

UserRepo.java

package com.springboot.repo;
import com.springboot.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Mapper
public interface UserRepo {
 int insert(User user);
 User selectByPrimaryKey(Long id);
 int updateByPrimaryKey(User user);
 int deleteByPrimaryKey(Long id);
 List<User> selectAll();
}

UserMapper.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.springboot.repo.UserRepo" >
 <resultMap id="BaseResultMap" type="com.springboot.entity.User" >
 <id column="id" property="id" jdbcType="BIGINT" />
 <result column="name" property="name" jdbcType="VARCHAR" />
 </resultMap>
 <sql id="Base_Column_List" >
 id,
 name
 </sql>
 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
 select
 <include refid="Base_Column_List" />
 from user
 where id = #{id,jdbcType=BIGINT}
 </select>
 <select id="selectAll" resultMap="BaseResultMap">
 select
 <include refid="Base_Column_List" />
 from user
 </select>
 <update id="updateByPrimaryKey" parameterType="com.springboot.entity.User" >
 update user
 <set>
  <if test="name != null" >
  `name`= #{name,jdbcType=VARCHAR},
  </if>
 </set>
 where id = #{id,jdbcType=BIGINT}
 </update>
 <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
 delete from user
 where id = #{id,jdbcType=BIGINT}
 </delete>
 <insert id="insert" parameterType="com.springboot.entity.User" useGeneratedKeys="true" keyProperty="id">
 insert into user
 <trim prefix="(" suffix=")" suffixOverrides="," >
  name
 </trim>
 <trim prefix="values (" suffix=")" suffixOverrides="," >
  #{name,jdbcType=VARCHAR}
 </trim>
 </insert>
</mapper>

編寫application.properties

在Spring Boot為我們生成的application.properties文件中添加如下內(nèi)容:

spring.datasource.name=spring_boot_study spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=xxxxxx spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.springboot.entity

單元測(cè)試

package com.springboot;
import com.springboot.entity.User;
import com.springboot.repo.UserRepo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {
 @Autowired
 private UserRepo userRepo;
 @Test
 public void testInsert() {
 User user = new User();
 user.setName("test2");
 userRepo.insert(user);
 }
 @Test
 public void testUpdate() {
 User user = new User();
 user.setId(6L);
 user.setName("test3");
 userRepo.updateByPrimaryKey(user);
 }
 @Test
 public void testDelete() {
 userRepo.deleteByPrimaryKey(6L);
 }
 @Test
 public void testSelectByPrimaryKey() {
 User user = userRepo.selectByPrimaryKey(7L);
 System.out.println(user);
 }
 @Test
 public void testSelectAll() {
 List<User> userList = userRepo.selectAll();
 System.out.println(userList);
 }
}

總結(jié)

以上所述是小編給大家介紹的Spring Boot集成Mybatis的實(shí)例代碼(簡(jiǎn)潔版),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論