SpringBoot整合Mybatis完成增删改查功能

1.我用的开发工具是Spring-tool-Suite。这个工具适合创建Spting类型的项目。(这篇文章开头有使用介绍:http://blog.csdn.net/baidu_36216018/article/details/79461016;使用IDEA工具也行,)

2.创建一个项目:

① new--->Spring Stater project


③:搜索需要的依赖包

④:我们发现上一步选择的依赖,在pom.xml文件中 已经自动添加到我们的文件中了。


2. 开始编写代码和一些配置

先看一下我的项目的最后目录结构:



①:自己创建一个数据表  字段为id , name, password ,number 四个字段   id为int类型,其他的都为String类型。

在src/main/java目录下添加controller层,entity层,mapper层,service层

entity层的实体类User:

package com.example.demo.entity;

public class User {
	
	private int id;
	private String name;
	private String password;
	private String number;
	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 getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}



	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password + ", number=" + number + "]";
	}


	
}

mapper层的UserMapper类:

package com.example.demo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.example.demo.entity.User;
@Mapper
public interface UserMapper {
	
	List<User> findUserByName(String name);
	
	public List<User> ListUser();
	
	public int insertUser(User user);
	
	public int delete(int id);
	
	public int Update(User user);
	
}

service层的实现类Userservice:

package com.example.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;



@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;

	public List<User> findByName(String name) {
		return userMapper.findUserByName(name);
	}

	public User insertUser(User user) {
		userMapper.insertUser(user);
		return user;
	}
	public List<User> ListUser(){
		return	userMapper.ListUser();
	}
	
	
	public int Update(User user){
		return userMapper.Update(user);
	}
	
	public int delete(int id){
		return userMapper.delete(id);
	}
}
controller层 的访问类CRUD:
package com.example.demo.controller;

import java.util.List;

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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;


@RestController
@RequestMapping(value = "/CRUD", method = { RequestMethod.GET, RequestMethod.POST })
public class CRUD {
	@Autowired
	private UserService userservice;

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(int id) {
		int result = userservice.delete(id);
		if (result >= 1) {
			return "删除成功";
		} else {
			return "删除失败";
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(User user) {
		int result = userservice.Update(user);
		if (result >= 1) {
			return "修改成功";
		} else {
			return "修改失败";
		}

	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public User insert(User user) {
		return userservice.insertUser(user);
		}
	
	@RequestMapping("/ListUser")
	@ResponseBody
	public List<User> ListUser(){
		return userservice.ListUser();
	}

	@RequestMapping("/ListUserByname")
	@ResponseBody
	public List<User> ListUserByname(String name){
		return userservice.findByName(name);
	}
}

接着:

在src/main/resources下写UserMapper的映射文件xml.

UserMapper.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD com.example.Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper">
	<resultMap id="result" type="com.example.demo.entity.User">
		<result property="name" column="name" />
		<result property="password" column="password" />
		<result property="number" column="number"/>

	</resultMap>

	<select id="ListUser" resultMap="result">
		SELECT * FROM user
	</select>

	<select id="findUserByName" resultMap="result">
		SELECT * FROM user where name=#{name}
	</select>

	<insert id="insertUser" parameterType="com.example.demo.entity.User"
		keyProperty="id" useGeneratedKeys="true">
		INSERT INTO user
		(
		id,name,password,number
		)
		VALUES (
		#{id},
		#{name, jdbcType=VARCHAR},
		#{password, jdbcType=VARCHAR},
		#{number}
		)
	</insert>
	
	<delete id="delete" parameterType="int">
		delete from user where id=#{id}
	</delete>
	
	<update id="Update" parameterType="com.example.demo.entity.User">
	update user set user.name=#{name},user.password=#{password},user.number=#{number} where user.id=#{id}
	</update>
</mapper>

最后配置application.properties配置文件:

spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 11111111
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.mapper-locations= classpath:mapper/*.xml

启动程序的入口类:SpringbootMybatisApplication.java

我们在postmain这个工具中测试:

测试查询所有的信息:http://localhost:8080/CRUD/ListUser




测试根据name查询数据:

http://localhost:8080/CRUD/ListUserByname?name=小王

测试根据Id删除数据:

http://localhost:8080/CRUD/delete?id=40


测试根据插入数据:http://localhost:8080/CRUD/insert?name=波波&password=123456&number=123





测试修改数据:http://localhost:8080/CRUD/update?id=46&name=zyb&password=9999999&number=5555



结束!源码地址-->码云:源码地址

  • 24
    点赞
  • 161
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值