栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

搭建springboot对mysql表增删改查

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

搭建springboot对mysql表增删改查

安装数据库环境

安装mysql,在window环境下启动,可以用sqlyong或其它工具连接数据库

net start mysql
cd C:Program FilesMySQLMySQL Server 5.6bin
mysql -u root -p
show databases;

创建表

CREATE TABLE `user` (
  `id` int(11) ,
  `name` varchar(50) ,
  PRIMARY KEY (`id`)
)

搭建Springboot环境

springboot的访问顺序:controller--Service(Interface)--MapperDao--MapperXML--数据库。

 

编辑配置文件 application.properties 、pom.xml

编辑 application.properties 文件

# 数据库设置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=test
spring.datasource.password=test

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.springboot.entity

编辑 pom.xml 文件



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		3.0.0-SNAPSHOT
		 
	
	com.example
	springboot
	0.0.1-SNAPSHOT
	springboot
	
		17
	
	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
		
            org.springframework.boot
            spring-boot-starter-web
        
        
        
         
         org.mybatis.spring.boot 
         mybatis-spring-boot-starter 
         1.3.2 
     
     
        mysql
        mysql-connector-java
    
    
    
	
	
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
			
			
		
	
	
	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
		
			spring-snapshots
			Spring Snapshots
			https://repo.spring.io/snapshot
			
				false
			
		
	
	
	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
		
			spring-snapshots
			Spring Snapshots
			https://repo.spring.io/snapshot
			
				false
			
		
	


编写实体类

package com.example.springboot.entity;

import org.springframework.stereotype.Component;

@Component
public class User {
	private int id;
	private String name;
	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;
	}
}

编写Controller

package com.example.springboot.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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;

@RestController
public class UserController {
	
	@RequestMapping("/")
	public String index() {
		
		return "index";
	}	
	
	@RequestMapping("/hello")
		public String Hello() {
			return "Hello,Springboot.";
		}
	
	@Autowired
	private UserService userService;

	@RequestMapping("/getUser")
	public List getUser(@RequestParam("id") int id) {
		return userService.getUser(id);
	}
	
	@RequestMapping("/addUser")
	public int addUser(@RequestParam("user") User user) {
		return userService.addUser(user);
	}
	
	@RequestMapping("/deleteUser")
	public void deleteUser(@RequestParam("id") int id) {
		userService.deleteUser(id);
	}
	
	@RequestMapping("/updateUser")
	public boolean updateUser(@RequestParam("user") User user) {
		return userService.updateUser(user);
	}
}

编写 Service 接口以及 Service 实现
package com.example.springboot.service;

import java.util.List;

import com.example.springboot.entity.User;

public interface UserService {
	public List getUser(int id);
	
	public int addUser(User user) ;
	
	public void deleteUser(int id);
	  
	public boolean updateUser(User user);
}
package com.example.springboot.service.impl;

import java.util.ArrayList;
import java.util.linkedList;
import java.util.List;

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

import com.example.springboot.dao.UserDao;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public List getUser(int id) {
		return userDao.getUser(id);
	}

	@Override
	public int addUser(User user) {
		// TODO Auto-generated method stub
		userDao.addUser(user);
		return 0;
	}

	@Override
	public void deleteUser(int id) {
		// TODO Auto-generated method stub
		userDao.deleteUser(id);
	}

	@Override
	public boolean updateUser(User user) {
		return userDao.updateUser(user);
	}
	
}

编写Dao层
package com.example.springboot.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

import com.example.springboot.entity.User;

@Mapper
public interface UserDao {
	  List getUser(int id);
	  
	  void addUser(User user);
	  
	  @Delete("delete from user where id = #{id}")
	  void deleteUser(int id);
	  
	  @Update("update user set name = #{name} where id = #{id}")
	  boolean updateUser(User user);
}

当然,也可以使用mapper.xml编写SQL语句,放置在/src/main/resources/mapper目录,与前面application.properties的配置文件对应

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.springboot.entity