前后端分离之SpringBoot2.x整合mybatis实现数据库的增删改查操作(一)

1.新建springBoot2.x项目,添加mybatis,mysql等依赖,太过基础,这里不多介绍。

项目目录结构如下:

前后端分离之SpringBoot2.x整合mybatis实现数据库的增删改查操作(一)_第1张图片
pom.xml文件如下



	4.0.0

	com.example
	demo1
	0.0.1-SNAPSHOT
	jar

	demo1
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.0.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		

		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			
			com.alibaba
			druid
			1.1.6
		
		
		
		  
		     org.springframework.boot  
		     spring-boot-devtools  
		     true  
		 
	
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



1.启动类开启包扫描,@MapperScan(“com.example.demo1.mapper”)
2.domain包 实体类User

package com.example.demo1.domain;

import java.util.Date;

public class User {
//	name,number,dgh,max_gh,weight
	private int id;
	
	private String name; //单板名称
	
	private int number;//数量
	
	private int dgh;  //典型功耗
	
	private int max_gh; //最大功耗
	
	private int weight; //重量

	
	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 int getNumber() {
		return number;
	}

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

	public int getDgh() {
		return dgh;
	}

	public void setDgh(int dgh) {
		this.dgh = dgh;
	}

	public int getMax_gh() {
		return max_gh;
	}

	public void setMax_gh(int max_gh) {
		this.max_gh = max_gh;
	}

	public int getWeight() {
		return weight;
	}

	public void setWeight(int weight) {
		this.weight = weight;
	}



	
	
	
}

3.mapper层,数据库查询操作

package com.example.demo1.mapper;
import com.example.demo1.domain.User;
import java.util.List;
/*
 * 功能描述: 访问数据库
 */

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;
public interface UserMapper {
	 @Insert("INSERT INTO user(name,number,dgh,max_gh,weight) VALUES(#{name}, #{number}, #{dgh},#{max_gh},#{weight})")
	 @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")   //keyProperty java对象的属性;keyColumn表示数据库的字段
	 int insert(User user);
	 
	 
	 
	
//
   @Select("SELECT * FROM user")
   @Results({
       @Result(column = "name",property = "name")  //javaType = java.util.Date.class        
   })
   List getAll();
// 
//   
//
   @Select("SELECT * FROM user WHERE id = #{id}")
   @Results({
   	 @Result(column = "name",property = "name")
   })
   User findById(Long id);
   
//通过单板名称进行查询
//   @Select("select * from user")
//   @Results(id="nameMap",value={
//		@Result(column = "name",property = "name") 	
//   })
//   List selectAll();
//   @Select({"select * from where name = #{name}"})
//   @ResultMap(value="nameMap")
//   User findByName( String name);
   
   @Select("SELECT * FROM user WHERE name = #{name}")
   @Results({
   	 @Result(column = "name",property = "name")
   })
   User findByName(String name); 
   
//通过单板数量进行查询
   @Select("SELECT * FROM user WHERE number = #{number}")
   @Results({
   	 @Result(column = "name",property = "name")
   })
   User findByNumber(int number);
   

//  
//
   @Update("UPDATE user SET number=#{number} WHERE id =#{id}")
   void update(User user);
//
   @Delete("DELETE FROM user WHERE id =#{userId}")
   void delete(Long userId);

	
}

4.1

package com.example.demo1.service;

import org.apache.ibatis.annotations.Param;

import com.example.demo1.domain.User;
public interface UserService {
     public int add(User user);
    
}

4.1继承service的iml,实现去耦合,不懂得建议去看下设计模式

package com.example.demo1.service.impl;

import java.util.Date;

import com.example.demo1.domain.User;
import com.example.demo1.mapper.UserMapper;
import com.example.demo1.service.UserService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService{

	 @Autowired
	 private UserMapper userMapper;
	 
	@Override
	public int add(User user) {
		userMapper.insert(user);
		int id = user.getId();
		return id;
	}
//	@Override
//	public String addName(User user) {
//		userMapper.insert(user);
//		String name = user.getName();
//		return name;
//	}
	
	
	
	

	
	
}

5.controller层

package com.example.demo1.controller;
import com.example.demo1.domain.JsonData;
import com.example.demo1.domain.User;
import com.example.demo1.mapper.UserMapper;
import com.example.demo1.service.UserService;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**

 */
@RestController
@RequestMapping("/api/v1/user")
public class UserController {
	
	
	@Autowired
	private UserService userService;
	
	
	/**
	 * 功能描述: user 保存接口
	 * @return
	 */
	@GetMapping("add")
	public Object add(){
		User user = new User();

		user.setDgh(100);
		user.setMax_gh(99);
		user.setName("8080_u_p");
		user.setNumber(98);
		user.setWeight(97);
		int id = userService.add(user);
		
       return JsonData.buildSuccess(id);
	}
	
	@Autowired
	private UserMapper userMapper;
	@GetMapping("findAll")
	public Object findAll(){
       return JsonData.buildSuccess(userMapper.getAll());
	}
//	
//	
//	通过id进行查询
	@GetMapping("findById")
	public Object findById(long id){
       return JsonData.buildSuccess(userMapper.findById(id));
	}
	//通过名称查询
	@GetMapping("findByName")
	public Object findByName(@Param("name")String name){
		
		System.out.println(userMapper.findByName(name));
		System.out.println(String.valueOf(name));
		System.out.println(name.toString());
       return JsonData.buildSuccess(userMapper.findByName(name));
       
	}
	
	//通过数量查询
	@GetMapping("findByNumber")
	public Object findByNumber(int number)
	{
		return JsonData.buildSuccess(userMapper.findByNumber(number));
		
	}
	
	
//	
	@GetMapping("del_by_id")
	public Object delById(long id){
	userMapper.delete(id);
       return JsonData.buildSuccess();
	}
//	
	@GetMapping("update")
	public Object update(int id,int number){
		User user = new User();
		user.setNumber(number);
		user.setId(id);
		userMapper.update(user);
	    return JsonData.buildSuccess();
	}
	
	@GetMapping("test")
	public String toString()
	{
		return "hello world";
	}
//	
	
	
	
	
	
//	//测试事务
//	@GetMapping("transac")
//	public Object transac(){
//		int id = userService.addAccount();
//	    return JsonData.buildSuccess(id);
//	}
//	
//	
	
	
}

后续请看前后端分离之SpringBoot2.x整合mybatis实现数据库的增删改查操作(二)

你可能感兴趣的:(Springboot)