Spring Boot JdbcTemplate MySQL

Spring Boot JdbcTemplate MySQL

1、配置pom.xml,增加jdbc、HikariCP连接池、MySQL驱动。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
  <groupId>com.zaxxer</groupId>
  <artifactId>HikariCP</artifactId>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

2、DataSourceConfig.java

package com.idodo.demo.conf;

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
public class DataSourceConfig {
  
  @Bean(name = "dataSource")
  public DataSource datasource(Environment env) {
    HikariDataSource ds = new HikariDataSource();
    ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
    ds.setUsername(env.getProperty("spring.datasource.username"));
    ds.setPassword(env.getProperty("spring.datasource.password"));
    ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
    return ds;
  }

}

3、User.java

package com.idodo.demo.entity;

import java.io.Serializable;

public class User implements Serializable{

  private static final long serialVersionUID = 7914100053493869907L;
  
  private Integer user_id ;
  private String user_code ;
  private String user_name ;
  
  public User() {
  }

  public Integer getUser_id() {
    return user_id;
  }

  public void setUser_id(Integer user_id) {
    this.user_id = user_id;
  }

  public String getUser_code() {
    return user_code;
  }

  public void setUser_code(String user_code) {
    this.user_code = user_code;
  }

  public String getUser_name() {
    return user_name;
  }

  public void setUser_name(String user_name) {
    this.user_name = user_name;
  }
  
}

4、UserService.java

package com.idodo.demo.service;

import com.idodo.demo.entity.User;

public interface UserService {
  public User geUserById(Integer user_id);
  
  public void test(Integer user_id);
  
}

5、UserServiceImpl.java

package com.idodo.demo.service.impl;

import java.util.List;

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

import com.idodo.demo.dao.UserDao;
import com.idodo.demo.entity.User;
import com.idodo.demo.service.UserService;

@Service
public class UserServiceImpl implements UserService {
  
  @Autowired UserDao userDao;
  
  public User geUserById(Integer user_id) {
    User user = userDao.findUserById(user_id);
    return user;
  }
  
  public void test(Integer user_id) {
    System.out.println(userDao.getUserTotal(user_id));
    
    List<User> list = userDao.getUserList("9002");
    for (User u : list) {
      System.out.println(u.getUser_name());
    }
    
    User user = new User();
    user.setUser_id(1);
    user.setUser_code("90001");
    user.setUser_name("jzhjzh");
    userDao.update(user);
    
    User vo = new User();
    vo.setUser_id(1);
    vo.setUser_code("9009");
    vo.setUser_name("jiangzhihao2018");
    userDao.insertUser(vo);
    
  }
}

6、UserController.java

package com.idodo.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idodo.demo.entity.User;
import com.idodo.demo.service.UserService;

@Controller
public class UserController {
  @Autowired
  UserService userService;
  
    // http://localhost:8080/user/1
  // {"user_id":1,"user_code":"9001","user_name":"jzh"}
  @RequestMapping("/user/{user_id}")
  public @ResponseBody User getUser(@PathVariable Integer user_id) {
    User user = userService.geUserById(user_id);
    
    // 测试
    userService.test(user_id);
    
    return user;
  }
}

 

发表回复

您的电子邮箱地址不会被公开。