Eclipse:SpringBoot+mybatis實現增刪改查

此項目代碼已上傳至github:https://github.com/snowlavenderlove/springMybatisProject

1.新建數據庫spring_mybatis

2.新建表user

表中字段id,name,password,id爲主鍵並自增

3.進入網址https://start.spring.io/新建springboot項目,此處可參考博文https://mp.csdn.net/postedit/90669242

4.將生成的zip壓縮包解壓,打開Eclipse->Import->Maven->Existing Maven Project

5.使用generator-mybatis自動生成數據庫對應類相關代碼,此處可參考 https://mp.csdn.net/postedit/90692784,此處不再贅述

6.將第五步生成的包複製到springMybatisProject中,如圖

7.編輯pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.xue</groupId>
	<artifactId>springMybatisProject</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springMybatisProject</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
		</dependency>



		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
			<version>5.1.28</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>druid</artifactId>
		    <version>1.0.27</version>
		</dependency>

		<dependency>
    		<groupId>commons-logging</groupId>
    		<artifactId>commons-logging</artifactId>
    		<version>1.2</version>
		</dependency>
		
		
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

8.編輯application.properties

#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/spring_mybatis
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
#druid datasource
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#mybatis
mybatis.type-aliases-package=com.xue.reprository.dao
mybatis.mapper-locations=classpath*:com/xue/repository/mapper/*.xml

9.在com.xue下創建controller包,並新建SysLoginController類

10.在com.xue下創建service包,在service包下創建sys包,並新建UserService接口,同時在sys包下創建新包Impl,在Impl下新建實現類UserServiceImpl如圖:

11.最終項目結構如圖:

 

12.編輯UserService.java

package com.xue.service.sys;

import java.util.List;

import com.xue.entity.model.User;

public interface UserService {
	
	//增
	public int addUser(User user);
	//刪
	public int deleteUser(int id);
	//改	
	public int updateUser(User user);
	//查	
	public List<User> getUser();
	
	

}

13.編輯UserServiceImpl.java

package com.xue.service.sys.Impl;

import java.util.List;

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

import com.xue.entity.model.User;
import com.xue.repository.dao.UserMapper;
import com.xue.service.sys.UserService;
@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper dao;
	
	//增
	@Override
	public int addUser(User user) {
		
		int result = 0;
		try{
			result = dao.insert(user);
		}catch(Exception e){
			
			e.printStackTrace();
		}
		
		return result;
	}

	
	//刪
	@Override
	public int deleteUser(int id) {
		
		int	result =0;
		try {
			result = dao.deleteByPrimaryKey(id);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return result;
	}
	//改
	@Override
	public int updateUser(User user) {
		int result = 0;
		
		try {
			result = dao.updateByPrimaryKey(user);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return result;
	}

	//查
	@Override
	public List<User> getUser() {
		
		return dao.selectAllInfo();
	}
	

}

 14.編輯UserMapper.java,在最後添加

15.編輯UserMapper.xml,在最後添加

16. 編輯sysLoginController.java

package com.xue.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.RestController;

import com.xue.entity.model.User;
import com.xue.service.sys.UserService;

@RestController
@RequestMapping("/sys/acct")
public class SysLoginController {
	
	@Autowired
	private UserService userService;
	
	//增
	@RequestMapping("/addUser")
	public int addUser(){
		
		//向數據庫添加兩條記錄
		
		User user = new User();
		user.setName("zhang san ");
		user.setPassword("123456");	
		
		User user1 = new User();
		user1.setName("li si");
		user1.setPassword("123456");	
		
		int result = userService.addUser(user);
		
		int result1 = userService.addUser(user1);
		
		return result & result1;
	}
	//刪
	@RequestMapping("/deleteUser")
	public int deleteUser(){
		
		//刪除數據中中第一條記錄
		int result = userService.deleteUser(1);
		
		return result;
	}
	
	@RequestMapping("/updataUser")
	public int updateUser(){
		
		//修改數據庫中第二條記錄
		
		User user = new User();
		user.setId(2);
		user.setName("chen er");
		user.setPassword("123");
		int result = userService.updateUser(user);	
		return result;
	}
	
	
	@RequestMapping("/showUser")
	public List<User> getUserInfo(){
		
		//查詢數據庫user表中所有記錄
		
		List<User> user = userService.getUser();
		
		return user;
		
	}
	

}

17.啓動類代碼

package com.xue;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.xue.repository.dao")
public class SpringMybatisProjectApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringMybatisProjectApplication.class, args);
	}

}

18.運行啓動類在瀏覽器輸入localhost:8080 /sys/acct/addUser,並查看數據庫如圖

19.在瀏覽器輸入localhost:8080 /sys/acct/deleteUser,並查看數據庫如圖

20.在瀏覽器輸入localhost:8080 /sys/acct/updateUser,並查看數據庫如圖

21.在瀏覽器輸入localhost:8080 /sys/acct/showUser如圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章