<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>

          精通 Spring Boot 系列文(11)

          共 4907字,需瀏覽 10分鐘

           ·

          2020-07-27 14:11

          閱讀全文,約 14?分鐘

          99f31bc638e844d9b45e7762af8fe745.webp


          精通 Spring Boot 系列文(1)精通 Spring Boot 系列文(2)精通 Spring Boot 系列文(2)精通 Spring Boot 系列文(4)精通 Spring Boot 系列文(5)精通 Spring Boot 系列文(6)精通 Spring Boot 系列文(7)精通 Spring Boot 系列文(8)精通 Spring Boot 系列文(9)精通 Spring Boot 系列文(10)

          Spring Boot 整合 MyBatis

          MyBatis 是目前優(yōu)秀的 ORM 框架,支持普通的數(shù)據(jù)庫操作,幾乎消除了常規(guī)的 JDBC 操作,極大簡化我們的開發(fā)操作。

          1)編輯 pom.xml 文件

          <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.nxgroupId>
          ????<artifactId>springbootdataartifactId>
          ????<version>1.0-SNAPSHOTversion>

          ????<parent>
          ????????<groupId>org.springframework.bootgroupId>
          ????????<artifactId>spring-boot-starter-parentartifactId>
          ????????<version>2.2.6.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.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-starter-thymeleafartifactId>
          ????????dependency>

          ????????
          ????????<dependency>
          ????????????<groupId>mysqlgroupId>
          ????????????<artifactId>mysql-connector-javaartifactId>
          ????????dependency>

          ????????
          ????????<dependency>
          ????????????<groupId>org.mybatis.spring.bootgroupId>
          ????????????<artifactId>mybatis-spring-boot-starterartifactId>
          ????????????<version>1.3.1version>
          ????????dependency>

          ????????<dependency>
          ????????????<groupId>junitgroupId>
          ????????????<artifactId>junitartifactId>
          ????????????<scope>testscope>
          ????????dependency>
          ????dependencies>
          project>

          2)編輯 application.properties 文件

          ####################
          ###?數(shù)據(jù)源信息配置?###
          ####################
          #?數(shù)據(jù)庫地址
          spring.datasource.url=jdbc:mysql://localhost:3306/springbootdata?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
          #?用戶名
          spring.datasource.username=root
          #?密碼
          spring.datasource.password=1234
          #?數(shù)據(jù)庫驅(qū)動
          spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
          #?指定連接池中最大的活躍連接數(shù).
          spring.datasource.max-active=20
          #?指定連接池最大的空閑連接數(shù)量.
          spring.datasource.max-idle=8
          #?指定必須保持連接的最小值
          spring.datasource.min-idle=8
          #?指定啟動連接池時,初始建立的連接數(shù)量
          spring.datasource.initial-size=10

          3)創(chuàng)建 User 持久化類

          public?class?User?implements?Serializable{

          ????private?static?final?long?serialVersionUID?=?1L;

          ????private?int?id?;
          ????private?String?loginName?;
          ????private?String?username?;
          ????private?String?password;

          ????//?setXxx?和?getXxx?方法
          }

          4)創(chuàng)建 UserRepository 數(shù)據(jù)訪問接口

          public?interface?UserRepository?{

          ????@Insert("insert?into?tb_user(login_name?,username?,password)?"
          ????????????????+?"values?(#{loginName},#{username},#{password})")
          ????public?int?insertUser(User?user);

          ????//?插入數(shù)據(jù)獲取主鍵
          ????@Insert("insert?into?tb_user(login_name?,username?,password)?"
          ????????????+?"values?(#{loginName},#{username},#{password})")
          ????@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
          ????public?void?insertGetKey(User?user);


          ????@Select("select?*?from?tb_user?where?username?=?#{username}")
          ????//?引用id="userResult"的@Results
          ????@ResultMap("userResult")
          ????public?User?selectByUsername(@Param("username")String?username);

          ????@Select("select?*?from?tb_user")
          ????//?@Results用于映射對象屬性和數(shù)據(jù)庫列,常用于對象屬性和數(shù)據(jù)庫列不同名情況
          ????@Results(id="userResult",value={
          ????????????@Result(id=true,column="id",property="id"),
          ????????????@Result(column="login_name",property="loginName"),
          ????????????@Result(column="password",property="password"),
          ????????????@Result(column="username",property="username")
          ????????})
          ????public?List?findAll();


          ????@Delete("delete?from?tb_user?where?id=#{id}")
          ????public?void?delete(final?Integer?id);


          ????@Select("select?*?from?tb_user?where?id=#{id}")
          ????//?引用id="userResult"的@Results
          ????@ResultMap("userResult")
          ????public?User?findUserById(int?id);

          ????@Update("update?tb_user?set?username=#{username},?login_name=#{loginName}?where?id=#{id}")
          ????public?void?update(final?User?user);
          }

          5)創(chuàng)建 UserService 業(yè)務(wù)層類

          @Service
          public?class?UserService?{

          ????//?注入UserRepository
          ????@Resource
          ????private?UserRepository?userRepository;

          ????public?int?insertUser(User?user){
          ????????return?userRepository.insertUser(user);
          ????}

          ????public?User?selectByUsername(String?username){
          ????????return?userRepository.selectByUsername(username);
          ????}

          ????public?List?findAll(){
          ????????return?userRepository.findAll();
          ????}

          ????public?void?insertGetKey(User?user)?{
          ????????userRepository.insertGetKey(user);
          ????}

          ????public?void?update(User?user)?{
          ????????userRepository.update(user);
          ????}

          ????public?void?delete(Integer?id)?{
          ????????userRepository.delete(id);
          ????}
          }

          6)創(chuàng)建 UserController 控制器類

          @RestController
          @RequestMapping("/user")
          public?class?UserController?{

          ????//?注入UserService
          ????@Resource
          ????private?UserService?userService;

          ????@RequestMapping("/insertUser")
          ????public?String?insertUser(User?user){
          ????????return?"插入數(shù)據(jù)["+userService.insertUser(user)+"]條";
          ????}

          ????@RequestMapping("/insertGetKey")
          ????public?User?insertGetKey(User?user)?{
          ????????userService.insertGetKey(user);
          ????????return?user?;
          ????}

          ????@RequestMapping("/selectByUsername")
          ????public?User?selectByUsername(String?username){
          ????????return?userService.selectByUsername(username);
          ????}

          ????@RequestMapping("/findAll")
          ????public?List?findAll(){
          ????????return?userService.findAll();
          ????}

          ????@RequestMapping("/update")
          ????public?void?update(User?user)?{
          ????????userService.update(user);
          ????}

          ????@RequestMapping("/delete")
          ????public?void?delete(Integer?id)?{
          ????????userService.delete(id);
          ????}
          }

          7)測試

          http://localhost:8080/user/insertUser?loginName=shuaishuai&username=小猿&password=123123

          未完待續(xù),等我下一篇?~~~e74d2e7012563a6b6d6b58d1ad8ccee8.webp


          Java后端編程

          更多Java推文,關(guān)注公眾號

          瀏覽 17
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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第三季歌词完整版 | 色色色色综合 |