怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)-創(chuàng)新互聯(lián)

怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比弋陽(yáng)網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式弋陽(yáng)網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋弋陽(yáng)地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。

首先我們先創(chuàng)建一個(gè)SpringBoot 項(xiàng)目。

怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)

數(shù)據(jù)庫(kù)連接配置

##數(shù)據(jù)庫(kù)連接配置(部署到哪臺(tái),對(duì)應(yīng)的ip需修改)
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?connectTimeout=1000&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver=com.mysql.jdbc.Driver

數(shù)據(jù)庫(kù)中的數(shù)據(jù)

怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)

環(huán)境配好之后,下面分別介紹一下通過注解或者通過xml映射的形式這兩種方法來使用MyBatis。

通過xml映射的形式

測(cè)試Bean

package com.example.demo.model;

public class User {
 private int id;
 private String name;
 private String sex;
 private int age;

 public User() {
 }

 public User(String name, String sex, int age) {
  this.name = name;
  this.sex = sex;
  this.age = age;
 }

 public User(int id, String name, String sex, int age) {
  this.id = id;
  this.name = name;
  this.sex = sex;
  this.age = age;
 }

 public int getId() {
  return id;
 }

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

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getSex() {
  return sex;
 }

 public void setSex(String sex) {
  this.sex = sex;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }
}

XML形式的具體操作

將mapper定義為接口,只定義方法。具體的實(shí)現(xiàn)在同名的xml文件中。

package com.example.demo.mapper;

import com.example.demo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface UserMapper {
 User getByName(@Param("name") String name);

 boolean insert(User user);

 boolean update(@Param("name") String name, @Param("sex") String sex, @Param("age") int age);

 void delete(@Param("name") String name);
}
<?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.example.demo.mapper.UserMapper">
 <select id="getByName" resultType="com.example.demo.model.User" parameterType="java.lang.String">
  SELECT * FROM tb_user WHERE name = #{name}
 </select>

 <insert id="insert" parameterType="com.example.demo.model.User" useGeneratedKeys="true">
  INSERT INTO tb_user(name, sex, age) VALUES(#{name}, #{sex}, #{age})
 </insert>

 <update id="update" parameterType="com.example.demo.model.User">
  UPDATE tb_user SET sex=#{sex}, age=#{age} WHERE name=#{name}
 </update>

 <delete id="delete" parameterType="java.lang.String">
  DELETE FROM tb_user WHERE name = #{name}
 </delete>
</mapper>

兩個(gè)文件通過mapper.xml文件中的 namespace 形成映射。

一般情況下,我們用到的資源文件(各種xml,properites,xsd文件等)都放在src/main/resources下面(springboot回到對(duì)應(yīng)的位置加載文件),利用maven打包時(shí),maven能把這些資源文件打包到相應(yīng)的jar或者war里。但是,有的時(shí)候我們習(xí)慣把它和Mapper.java放一起,都在src/main/java下面,這樣利用maven打包時(shí),就需要修改pom.xml文件,來把mapper.xml文件一起打包進(jìn)jar或者war里了,否則,這些文件不會(huì)被打包的。(maven認(rèn)為src/main/java只是java的源代碼路徑)。

所以說,如果要將mapper.java和mapper.xml文件放在同一個(gè)位置,就需要在pom文件中指定xml文件的加載位置。

怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)

 <build>
 <resources> 
  <!-- maven項(xiàng)目中src源代碼下的xml等資源文件編譯進(jìn)classes文件夾,
   注意:如果沒有這個(gè),它會(huì)自動(dòng)搜索resources下是否有mapper.xml文件,
   如果沒有就會(huì)報(bào)org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): ... -->
  <resource> 
  <directory>src/main/java</directory> 
  <includes> 
   <include>**/*.xml</include> 
  </includes> 
  <filtering>true</filtering>
  </resource>
  
  <!--將resources目錄下的配置文件編譯進(jìn)classes文件 --> 
  <resource>
   <directory>src/main/resources</directory>
   <filtering>true</filtering>
  </resource>
 </resources> 
 </build>

如果mapper.java和mapper.xml文件是分開放置的,則不需要以上配置。

怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)

Service服務(wù)

package com.example.demo.service;

import com.example.demo.model.User;

public interface UserService {
 User getUserByName(String name);

 boolean addUser(User user);

 boolean updateUser(String name, String sex, int age);

 void deleteUser(String name);
}

Service服務(wù)的實(shí)現(xiàn)類

package com.example.demo.service.impl;

import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{
 @Autowired
 UserMapper userMapper;

 @Override
 public User getUserByName(String name) {
  User user = userMapper.getByName(name);
  if (null != user){
   return user;
  }
  return null;
 }

 @Override
 public boolean addUser(User user) {
  return userMapper.insert(user);
 }

 @Override
 public boolean updateUser(String name, String sex, int age) {
  return userMapper.update(name, sex, age);
 }

 @Override
 public void deleteUser(String name) {
  userMapper.delete(name);
 }
}

測(cè)試接口

package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
 @Autowired
 UserService userService;

 @RequestMapping(value = "/index", method = RequestMethod.GET)
 public String index(){
  User user = userService.getUserByName("gyl");
  return user.getName()+"--"+user.getSex()+"--"+user.getAge();
 }
}

如果一切順利,即將輸入localhost:8080/index 你將看到如下內(nèi)容

怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)

通過注解的方式

package com.example.demo.mapper;

import com.example.demo.model.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {

 @Select("select * from TB_USER where NAME = #{name}")
 User getByName(@Param("name") String name);

 @Insert("insert into TB_USER(NAME, SEX, AGE) values(#{name}, #{sex}, #{age})")
 boolean insert(User user);

 @Update("update TB_USER set SEX=#{sex}, AGE=#{age} where NAME=#{name}")
 boolean update(@Param("name") String name, @Param("sex") String sex, @Param("age") int age);

 @Delete("delete from TB_USER where NAME = #{name}")
 void delete(@Param("name") String name);
}

如果一切順利,即將輸入localhost:8080/index 你將看到如下內(nèi)容

怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)

關(guān)于怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

分享標(biāo)題:怎么在SpringBoot中使用MyBatis操作數(shù)據(jù)-創(chuàng)新互聯(lián)
文章源于:http://muchs.cn/article16/cospdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷標(biāo)簽優(yōu)化、建站公司、網(wǎng)站內(nèi)鏈、外貿(mào)網(wǎng)站建設(shè)小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)