- springboot mybatis+mapper.xml 零基
- 2018最新SpringBoot2.0教程(零基础入门)
- 零基础学SpringBoot(一)--初识SpringBoot
- SpringBoot2.0文章汇总目录,java多线程教程文章汇
- 【SpringBoot2.0系列05】SpringBoot之整合
- 【SpringBoot2.0系列04】SpringBoot之使用
- 【SpringBoot2.0系列03】SpringBoot之使用
- 【SpringBoot2.0系列02】SpringBoot之使用
- 【SpringBoot2.0系列11】SpringBoot之@E
- Java程序员力荐的PDF文档——深入浅出springboot,
前言:
各位同学大家好, 有段时间没有给大家更新博客, 具体多久我就不记得了。 今天我们讲一下spingboot +mybatis+xml
零基础教程
准备工作
1安装好idea 或者eclispe +sts开发环境
2安装maven 并配置环境
怎么使用idea 这个工具一键创建springboot工程
这些在我之前的教程都讲的很清楚 这里我就不展开细说 有兴趣的同学可以去看我以前的文章】
Springboot搭建零基础教程:https://www.jianshu.com/p/c48595fdbf39
需要用到三方库
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--java -web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--gson-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<!--log4j2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
</dependency>
application.ymal 的配置 如下图:
server:
port: 8085
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mybdtisdemo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true
username: root
password: 123456
#配置Mapper.xml映射文件
mybatis:
type-aliases-package: com.example.mybatisxmldmeo # 别名
mapper-locations: classpath:/mybatis/*.xml # xml文件
准备按设置了utf-8编码和 serverTimezone=UTC 和时区 以及数据库账号和密码 jdbc 驱动 这些都配置好以后我们启动一下

我们测试启动

我们看到有数据返回了
具体实现
-
1创建bean类(数据模型)
package com.example.mybatisxmldmeo.bean;
public class User {
private Integer id;
private String name;
private String password;
private String age;
private String sex;
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
-
2创建 Mapper类
package com.example.mybatisxmldmeo.dao;
import com.example.mybatisxmldmeo.bean.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository // 将userdao交由spring容齐管理
public interface UserDao {
public List<User> getallUser();
public User getuserbyId(Integer id);
int addUser(User user);
public User getuserbyname(String name);
public int upDatePassword(@Param("name") String name,
@Param("password") String password);
int deleteUser(@Param("id")Integer id);
public List<User>getallUserlimit(@Param("curPage") Integer curPage,@Param("pageSize") Integer pageSize);
public List<User>getallUserLike(@Param("name") String name);
List<User>getUserbyNameAndsex(@Param("name") String name, @Param("sex")String sex);
List<User>getUserByNameAndAge(@Param("name") String name ,@Param("newname")String newname,
@Param("age")String age);
}
-
3 UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatisxmldmeo.dao.UserDao">
<!--查询整个表-->
<select id="getallUser" resultType="com.example.mybatisxmldmeo.bean.User">
select * from user;
</select>
<!--根据头id 查询-->
<select id="getuserbyId" parameterType="int" resultType="com.example.mybatisxmldmeo.bean.User">
select * from user where id=#{id}
</select>
<!--删除数据-->
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
<!--插入数据-->
<insert id="addUser" parameterType="com.example.mybatisxmldmeo.bean.User">
insert into user(name, password,age,sex) values (#{name},#{passwrod},#{age},#{sex})
</insert>
</mapper>
-
3创建service 层
package com.example.mybatisxmldmeo.service;
import com.example.mybatisxmldmeo.bean.User;
import java.util.List;
public interface UserService {
public List<User> getallUser();
public User getuserbyId(Integer id);
int addUser(User user);
public User getuserbyname(String name);
public String upDatePassword(String name, String password, String newpsw);
int deleteUser(Integer id);
public List<User>getallUserlimit(Integer curPage,Integer pageSize);
public List<User>getallUserLike( String name);
List<User>getUserbyNameAndsex(String name,String sex);
List<User>getUserByNameAndAge( String name ,String newname,
String age);
}
-
4处理service 层 实现层 impl层
package com.example.mybatisxmldmeo.service.impl;
import com.example.mybatisxmldmeo.bean.User;
import com.example.mybatisxmldmeo.dao.UserDao;
import com.example.mybatisxmldmeo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "userService")
public class UserServiceimpl implements UserService {
@Autowired
UserDao userDao;
@Override
public List<User> getallUser() {
return userDao.getallUser();
}
/**
* @param id
* @return
* 通过头id查询数据
*
*
*/
@Override
public User getuserbyId(Integer id) {
return userDao.getuserbyId(id);
}
/**
* @param user
* @return
* 添加数据
*/
@Override
public int addUser(User user) {
return userDao.addUser(user);
}
//通过用户名查询数据
@Override
public User getuserbyname(String name) {
return null;
}
/**
* @param name
* @param password
* @return
* 更新数据
*
*/
@Override
public String upDatePassword(String name, String password, String newpsw) {
User user=userDao.getuserbyname(name);
if(user!=null){
if(user.getPassword().equals(password)){
userDao.upDatePassword(name,newpsw);
return "success";
}else{
return "defeated";
}
}else{
return "fail";
}
}
@Override
public int deleteUser(Integer id) {
return userDao.deleteUser(id);
}
/**
*
* @param curPage
* @param pageSize
* @return
* 分页查询
*
*/
@Override
public List<User> getallUserlimit(Integer curPage, Integer pageSize) {
return userDao.getallUserlimit(curPage,pageSize);
}
/**
* @param name
* @return
* 模糊查询
*
*/
@Override
public List<User> getallUserLike(String name) {
return userDao.getallUserLike(name);
}
/**
* @param name
* @param sex
* @return
* 多条件查询 需要传入 name sex等字段变量
*
*/
@Override
public List<User> getUserbyNameAndsex(String name, String sex) {
return userDao.getUserbyNameAndsex(name,sex);
}
/***
*
*
* @param name
* @param newname
* @param age
* @return
*/
@Override
public List<User> getUserByNameAndAge(String name, String newname, String age) {
return userDao.getUserByNameAndAge(name,newname,age);
}
}
controller 层具体业务逻辑实现:
-
查询所有数据
@RequestMapping("/getalluser")
public Object getAllUser(){
List<User>data=userservice.getallUser();
Map<String,Object>map=new HashMap<>();
if(data!=null&&data.size()>0){
map.put("code",200);
map.put("msg","获取数据成功");
map.put("data",data);
}else{
map.put("code",100);
map.put("msg","暂时没有数据");
}
return map;
}
我们打开postman测试请求一下

2.2查询单条数据 根据头id区查询数据
@RequestMapping("/getusetbyid")
public Object getUserById(@RequestParam(value = "id") Integer id){
User user=userservice.getuserbyId(id);
Map<String,Object>map=new HashMap<>();
if(user!=null){
map.put("code",200);
map.put("msg","获取数据成功");
map.put("user",user);
}else{
map.put("code",100);
map.put("msg","暂时没有数据");
}
return map;
}

删除单条数据 根据头id区删除数据
@RequestMapping("/deleteuser")
public Object deleteUser(@RequestParam(value = "id")Integer id){
Map<String,Object>map=new HashMap<>();
User user=userservice.getuserbyId(id);
if(user!=null){
int deletecode=userservice.deleteUser(id);
if(deletecode==1){
map.put("code",200);
map.put("msg","删除数据成功");
}else {
map.put("code",100);
map.put("msg","删除数据失败");
}
}else{
map.put("code",101);
map.put("msg","不存在该条数据");
}
return map;
}
查询删除之前数据

查看删除效果

查询删除之后的数据

最后总结
mybatis相对于过去 java web jsp和简单的SpringDataJPA 框架算是一个折中的方案 之前有讲到用注解方式访问数据库 现在这边改成xml形式来编写sql 语句 然后配合springboot+ mybatis 来访问数据库
当然你也可以用mybatis 逆向工程xml的形势来写业务逻辑的sql语句 这里篇幅有限我就不展开讲了,我们后期会讲到, 有兴趣的同学可以底下留言, 最后希望我的文章能帮助到各位解决问题 ,以后我还会贡献更多有用的代码分享给大家。各位同学如果觉得文章还不错 ,麻烦给关注和star,小弟在这里谢过啦
网友评论