用springboot写出简单的增删改查(controller层,service层,dao层,配置,依赖)

用springboot写出简单的增删改查(controller层,service层,dao层,配置,依赖)

原文连接
添加链接描述

框架:springboot 数据源:druid 数据库:mysql

controller层
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;

//增
@RequestMapping("/insertUser")
public void insertUser(User user) {
    userService.insertUser(user);
}

//删
@RequestMapping("/deleteUser")
public void deleteUser(int id) {
    userService.deleteUser(id);
}

@RequestMapping("/updateUser")
public void updateUser(User user) {
    userService.updateUser(user);
}

//查
@RequestMapping("/selectUser")
@ResponseBody
public List selectUser() {
    List users = userService.selectUser();
    return users;
}

}

service层
public interface UserService {
//增
void insertUser(User user);
//删
void deleteUser(int id);
//改
void updateUser(User user);
//查
List selectUser();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;

@Override
public void insertUser(User user) {
    userDao.insertUser(user);
}

@Override
public void deleteUser(int id) {
    userDao.deleteUser(id);
}

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

@Override
public List selectUser() {
    List users = userDao.selectUser();
    return users;
}

}
dao层
接口

public interface UserDao {
//增
void insertUser(User user);
//删
void deleteUser(int id);
//改
void updateUser(User user);
//查
List selectUser();
}
UserMapper.xml

               
    
    INSERT INTO USER (NAME,PASSWORD) VALUES (#{name},#{password})




    DELETE FROM USER WHERE id=#{id}




    update USER
    
        
            NAME = #{name},
        
        
            PASSWORD = #{password},
        
    
    WHERE id = #{id}




entity public class User { private Integer id; private String name; private String password;
public User() {
}

public User(Integer id, String name, String password) {
    this.id = id;
    this.name = name;
    this.password = password;
}

public Integer getId() {
    return id;
}

public void setId(Integer 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;
}

@Override
public String toString() {
    return "User{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", password='" + password + '\'' +
            '}';
}

}
配置
yml

server:
port: 9090 #端口号
servlet:
context-path: /yn #项目名

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver #数据库驱动
url: jdbc:mysql://127.168.17.0:3306/test #指定mysql数据库中的test子库
type: com.alibaba.druid.pool.DruidDataSource #数据源
username: root #用户名
password: root #密码

mybatis:
mapper-locations: classpath:com/yn/mapper/*.xml #注册映射文件
type-aliases-package: com.yn.entity #实体类定义别名
mvc: #配置视图解析器
view:
prefix: /
suffix: .jsp
入口类扫描dao

//扫描dao
@MapperScan(“com.yn.dao”)
@SpringBootApplication
public class DemoApplication {

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

}
依赖


org.springframework.boot
spring-boot-starter

    
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.0.0
    
    
    
        com.alibaba
        druid
        1.0.29
    
    
    
        mysql
        mysql-connector-java
        8.0.13
    
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
        
            
                org.junit.vintage
                junit-vintage-engine
            
        
    
    
        org.mybatis
        mybatis-spring
        2.0.2
        compile
    
    
        org.springframework.boot
        spring-boot-starter-web
    



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

你可能感兴趣的:(spring,boot,java,spring)