springboot+mybatis+mysql(增删改查xml入门编程)

jdk1.8

idea 2017.2.6

springboot 2.x

项目搭建省略,直接上代码:

项目结构:


springboot+mybatis+mysql(增删改查xml入门编程)_第1张图片

application.properties配置


server.port=8086

#加载Mybatis配置文件
#注意:一定要对应mapper映射xml文件的所在路径
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.type-aliases-package= com.java.mbt.mapper

#数据源必填项
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username = root
spring.datasource.password = mysql

#选填
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000
# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=50
# Validate the connection before borrowing it from the pool.
spring.datasource.tomcat.test-on-borrow=true

pojo类


package com.java.mbt.modle;

import java.io.Serializable;

/**
 * @authorseerhuitao 实体类用户描述
 * @create2019/5/9
 */
public class User implements Serializable {
    private Integer userId;

    private String userName;

    private String userAge;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserAge() {
        return userAge;
    }

    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", userAge='" + userAge + '\'' +
                '}';
    }
}

UserMapper接口:


package com.java.mbt.mapper;

import com.java.mbt.modle.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    int addUser(User user);

    int deleteUser(User user);

    int updateUser(User user);

    User getUserById(User user);

    List selectAllUser();

}

UserService接口:


package com.java.mbt.service;

import com.java.mbt.modle.User;

import java.util.List;

public interface UserService {
    int addUser(User user);

    int deleteUser(User user);

    int updateUser(User user);

    User getUserById(User user);

    List findAllUser();
}

UserServiceImpl类:


package com.java.mbt.service.impl;

import com.java.mbt.mapper.UserMapper;
import com.java.mbt.modle.User;
import com.java.mbt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpI implements UserService{
    //@Autowired(required=true):当使用@Autowired注解的时候,其实默认就是@Autowired(required=true),表示注入的时候,该bean必须存在,否则就会注入失败。
    //@Autowired(required=false):表示忽略当前要注入的bean,如果有直接注入,没有跳过,不会报错。
    @Autowired(required = false)
    //这里不写(required = false)底下的userMapper会报错
    private UserMapper userMapper;

    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public int deleteUser(User user){
        return userMapper.deleteUser(user);
    }

    @Override
    public  int updateUser(User user){
        return userMapper.updateUser(user);
    }

    @Override
    public  User getUserById(User user){
        return userMapper.getUserById(user);
    }

    @Override
    public List findAllUser() {
        return userMapper.selectAllUser();
    }

}

UserMapper.xml






    
    
    
        insert  into tb_user(user_name,user_age)
        values (#{userName},#{userAge})
    

    
        delete from tb_user where user_id = #{userId}
    

    
        update  tb_user set user_name =#{userName},user_age = #{userAge}  where  user_id= #{userId}
    

    

    

UUCotroller类:


package com.java.mbt.cotroller;

import com.java.mbt.modle.User;
import com.java.mbt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


//注意这里要是使用了RestCotroller,就不需要@ResponseBody,看起来还是RestCotroller比较方便
@RestController
//@ResponseBody
public class UUCotroller {
    @Autowired(required = false)
    private UserService userService;

    //测试
    @RequestMapping(value="/ff")
    public String getTest(){
        return "我是谁";
    }
    /*增加用户*/
    // http://localhost:8080/user/addUser/?userName=Alex&userAge=20
    @RequestMapping(value = "/addUser", produces = {"application/json;charset=UTF-8"})
   //    @ResponseBody
    public int addUser(User user){
        User u=new User();
        u.setUserAge("14");
        u.setUserName("恵涛");
//      return userService.addUser(user);
        return userService.addUser(u);
    }

    /*删除用户*/
    // http://localhost:8086/user/deleteUser/?userId=2
    @RequestMapping(value = "/deleteUser",produces = {"application/json;charset=UTF-8"})
//    @ResponseBody
    public  int deleteUser(User user)
    {
        return userService.deleteUser(user);
    }

    /*修改用户*/
    // http://localhost:8086/user/updateUser/?userId=3&userName=刘牧师&userAge=50
    @RequestMapping(value = "/updateUser",produces = {"application/json;charset=UTF-8"})
//    @ResponseBody
    public  int  updateUser(User user){
        return userService.updateUser(user);
    }

    /*根据UserId查找用户*/
    // http://localhost:8086/user/getUserById/?userId=3
    @RequestMapping(value = "/getUserById", produces = {"application/json;charset=UTF-8"})
//    @ResponseBody
    public User getUserById(User user){
        return userService.getUserById(user);
    }

    /*查询所有用户*/
    // http://localhost:8086/user/getAllUsers
    @RequestMapping(value = "/getAllUsers", produces = {"application/json;charset=UTF-8"})
//    @ResponseBody
    public Object findAllUser(){
        return userService.findAllUser();
    }

}

application类:


package com.java.mbt;

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

@SpringBootApplication
@MapperScan("com.java.mbt.mapper")
public class MbtApplication {

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

}

pow.xml




	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.4.RELEASE
		 
	
	com.java
	mbt
	0.0.1-SNAPSHOT
	mbt
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			2.0.1
		

		
			mysql
			mysql-connector-java
			5.1.34
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

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


数据库:

 

springboot+mybatis+mysql(增删改查xml入门编程)_第2张图片

 

接口演示:

springboot+mybatis+mysql(增删改查xml入门编程)_第3张图片

注意一般会犯的错误是:namespace的命名一般是mapper的包名+对应Mapper.XML的名字,去掉xml。

 

你可能感兴趣的:(【java亲测可用】)