今天给大家整理SpringBoot集成Mybatis用法笔记。希望对大家能有所帮助!
具体可以参考SpringBoot:搭建第一个Web程序
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
创建一个Mysql数据库,数据库名为test,然后执行一下脚本。
- /*
- 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');
- 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
- 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 +
- '}';
- }
- }
- 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.创建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}
- 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);
- }
- 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);
- }
- }
- 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对象转为json格式的数据
- 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
本文转载自微信公众号「IT技术分享社区」,可以通过以下二维码关注。转载本文请联系IT技术分享社区公众号。
博客链接:https://programmerblog.xyz
分享文章:SpringBoot集成Mybatis用法笔记
链接地址:http://www.shufengxianlan.com/qtweb/news38/347888.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联