新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBoot集成Mybatis用法筆記
今天給大家整理SpringBoot集成Mybatis用法筆記。希望對大家能有所幫助!

1.搭建一個SpringBoot基礎(chǔ)項目。
具體可以參考SpringBoot:搭建第一個Web程序
2.引入相關(guān)依賴
org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.3 mysql mysql-connector-java org.springframework.boot spring-boot-starter-test test junit junit 4.12 test
3.準(zhǔn)備數(shù)據(jù)庫腳本
創(chuàng)建一個Mysql數(shù)據(jù)庫,數(shù)據(jù)庫名為test,然后執(zhí)行一下腳本。
- /*
- Navicat MySQL Data Transfer
- Source Server : 本地MYSQL
- Source Server Version : 50644
- Source Host : localhost:3306
- Source Database : test
- Target Server Type : MYSQL
- Target Server Version : 50644
- File Encoding : 65001
- Date: 2021-05-16 17:20:26
- */
- SET FOREIGN_KEY_CHECKS=0;
- -- ----------------------------
- -- Table structure for t_user
- -- ----------------------------
- DROP TABLE IF EXISTS `t_user`;
- CREATE TABLE `t_user` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `user_name` varchar(255) CHARACTER SET armscii8 DEFAULT NULL,
- `password` varchar(255) CHARACTER SET armscii8 DEFAULT NULL,
- `last_login_time` datetime DEFAULT NULL,
- `sex` tinyint(4) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
- -- ----------------------------
- -- Records of t_user
- -- ----------------------------
- INSERT INTO `t_user` VALUES ('1', 'xiaoxin', '123', '2019-07-27 16:01:21', '1');
- INSERT INTO `t_user` VALUES ('2', 'jack jo', '123', '2019-07-24 16:01:37', '1');
- INSERT INTO `t_user` VALUES ('4', 'landengdeng', '123', '2019-07-24 16:01:37', '1');
- INSERT INTO `t_user` VALUES ('5', 'max', '123', '2019-07-24 16:01:37', '1');
- INSERT INTO `t_user` VALUES ('6', 'liua11', '123456', null, '1');
- INSERT INTO `t_user` VALUES ('7', 'xiaozhang', '888888', null, '1');
4.配置項目配置文件 application.yml
- server:
- port: 8090
- mybatis:
- configuration:
- map-underscore-to-camel-case: true
- mapper-locations: mybatis/**/*Mapper.xml
- spring:
- datasource:
- driverClassName: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8
- username: root
- password: root
- logging:
- level:
- my.springboot.mybatis.dao: debug
5.創(chuàng)建實體類 UserDO.java
- package my.springboot.mybatis.entity;
- import java.util.Date;
- public class UserDO {
- private Integer id;
- private String userName;
- private String password;
- private Integer sex;
- private Date lastLoginTime;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Integer getSex() {
- return sex;
- }
- public void setSex(Integer sex) {
- this.sex = sex;
- }
- public Date getLastLoginTime() {
- return lastLoginTime;
- }
- public void setLastLoginTime(Date lastLoginTime) {
- this.lastLoginTime = lastLoginTime;
- }
- @Override
- public String toString() {
- return "UserDO{" +
- "id=" + id +
- ", userName='" + userName + '\'' +
- ", password='" + password + '\'' +
- ", sex=" + sex +
- ", lastLoginTime=" + lastLoginTime +
- '}';
- }
- }
6.創(chuàng)建mapper文件 UserInfoMapper.java
- package my.springboot.mybatis.dao;
- import java.util.List;
- import java.util.Map;
- import my.springboot.mybatis.entity.UserDO;
- import org.apache.ibatis.annotations.Mapper;
- @Mapper
- public interface UserInfoMapper {
- UserDO get(Integer id);
- List
list(Map map); - int count(Map
map); - int save(UserDO user);
- int update(UserDO user);
- int remove(Integer id);
- int batchRemove(Integer[] ids);
- }
7.創(chuàng)建Mapper映射文件 UserInfoMapper.xml
- select id,user_name,password,last_login_time,sex from t_user where id = #{value}
- select id,user_name,password,last_login_time,sex from t_user
and id = #{id} and user_name = #{userName} and password = #{password} and last_login_time = #{lastLoginTime} and sex = #{sex} - order by ${sort} ${order}
- order by id desc
- limit #{offset}, #{limit}
- select count(*) from t_user
and id = #{id} and user_name = #{userName} and password = #{password} and last_login_time = #{lastLoginTime} and sex = #{sex} - insert into t_user
- (
- user_name,
- password,
- last_login_time,
- sex
- )
- values
- (
- #{userName},
- #{password},
- #{lastLoginTime},
- #{sex}
- )
- update t_user
user_name = #{userName}, password = #{password}, last_login_time = #{lastLoginTime}, sex = #{sex} - where id = #{id}
- delete from t_user where id = #{value}
- delete from t_user where id in
- #{id}
8.創(chuàng)建服務(wù)接口 IUserInfoService.java
- package my.springboot.mybatis.service;
- import my.springboot.mybatis.entity.UserDO;
- import java.util.List;
- public interface IUserInfoService {
- List
findAll(); - UserDO findById(Integer id);
- void insert(UserDO model);
- Integer update(UserDO model);
- Integer deleteById(Integer id);
- }
9.創(chuàng)建服務(wù)實現(xiàn)類 UserInfoService.java
- package my.springboot.mybatis.service.impl;
- import my.springboot.mybatis.dao.UserInfoMapper;
- import my.springboot.mybatis.entity.UserDO;
- import my.springboot.mybatis.service.IUserInfoService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.List;
- @Service
- public class UserInfoService implements IUserInfoService {
- @Autowired
- private UserInfoMapper mapper;
- @Override
- public List
findAll() { - return mapper.list(null);
- }
- @Override
- public UserDO findById(Integer id) {
- return mapper.get(id);
- }
- @Override
- public void insert(UserDO model) {
- mapper.save(model);
- }
- @Override
- public Integer update(UserDO model) {
- return mapper.update(model);
- }
- @Override
- public Integer deleteById(Integer id) {
- return mapper.remove(id);
- }
- }
10.創(chuàng)建控制器 HomeController.java
- package my.springboot.mybatis.controller;
- import my.springboot.mybatis.entity.UserDO;
- import my.springboot.mybatis.service.IUserInfoService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import javax.jws.soap.SOAPBinding;
- import java.util.Date;
- @Controller
- public class HomeController {
- @Autowired
- private IUserInfoService userInfoService;
- @RequestMapping("index") //注解映射請求路徑
- @ResponseBody //可以將java對象轉(zhuǎn)為json格式的數(shù)據(jù)
- public String index()
- {
- UserDO user=userInfoService.findById(1);
- // 新增用戶
- UserDO add=new UserDO();
- add.setSex(1);
- add.setUserName("xiaozhang");
- add.setPassword("888888");
- add.setLastLoginTime(null);
- //userInfoService.insert(add);
- // 更新用戶
- user.setUserName("xiaoxin");
- //userInfoService.update(user);
- // 刪除用戶
- userInfoService.deleteById(3);
- return "Hello World !";
- }
- }
啟動地址:http://localhost:8090/index
11.項目結(jié)構(gòu)文件截圖
本文轉(zhuǎn)載自微信公眾號「IT技術(shù)分享社區(qū)」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系IT技術(shù)分享社區(qū)公眾號。
博客鏈接:https://programmerblog.xyz
網(wǎng)站名稱:SpringBoot集成Mybatis用法筆記
網(wǎng)站地址:http://m.fisionsoft.com.cn/article/dhicodp.html


咨詢
建站咨詢
