SpringBoot中利用MyBatis進行資料操作
本例所用環境:
SpringBoot SQL/">MySQL MyBatis jdk1.8 Maven
首先我們先建立一個SpringBoot 專案。
資料庫連線配置 1 2 3 4 5 ##資料庫連線配置(部署到哪臺,對應的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 資料庫中的資料
環境配好之後,下面分別介紹一下通過註解或者通過xml對映的形式這兩種方法來使用MyBatis。
通過xml對映的形式 測試Bean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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定義為介面,只定義方法。具體的實現在同名的xml檔案中。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Service服務 1 2 3 4 5 6 7 8 9 10 11 12 13 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服務的實現類 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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{@AutowiredUserMapper userMapper;@Overridepublic User getUserByName(String name) {User user = userMapper.getByName(name);if (null != user){return user;}return null;}@Overridepublic boolean addUser(User user) {return userMapper.insert(user);}@Overridepublic boolean updateUser(String name, String sex, int age) {return userMapper.update(name, sex, age);}@Overridepublic void deleteUser(String name) {userMapper.delete(name);} } 測試介面 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 {@AutowiredUserService 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 你將看到如下內容
通過註解的方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 你將看到如下內容