1. 新建一个maven工程
参照SpringBoot(二)
2. 配置pom.xml
<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>
<groupId>it.source</groupId>
<artifactId>SpringBootJDBC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootJDBC Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 将当前项目作为springboot-starter的一个子模块 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<!-- 完成全局配置的替换、覆盖 -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入相关的能力,只使用web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
</project>
3. 配置application.properties
主要配置数据库信息
server.port=80
spring.datasource.url=jdbc:mysql://locahost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.dbcp.max-active=20
spring.datasource.dbcp.max-idle=8
spring.datasource.dbcp.min-idle=8
spring.datasource.dbcp.initial-size=10
4. Dao层代码(UserDao.java)
package com.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.itsource.domain.User;
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void save(User user) {
jdbcTemplate.update("insert into t_user(name) values(?)", user.getName());
}
public User get(Long id) {
RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
User user = jdbcTemplate.queryForObject("select * from t_user where id = ?",rowMapper,id);
return user;
}
}
5. Domain代码(User.java)
package com.domain;
public class User {
private long id;
private String name;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name) {
super();
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
6. Service层代码(接口和实现类)
接口
package com.service;
import com.itsource.domain.User;
public interface UserService {
void save(User user);
User get(Long id);
}
实现类
package com.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itsource.dao.UserDao;
import com.itsource.domain.User;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao dao;
@Override
public void save(User user) {
dao.save(user);
}
@Override
public User get(Long id) {
return dao.get(id);
}
}
7. Controller层代码(UserController.java)
package com.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itsource.domain.User;
import com.itsource.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user/save")
public String save(String name) {
User user = new User(name);
userService.save(user);
return "save successfully";
}
@RequestMapping("/user/get")
public User get(Long id) {
User user = userService.get(id);
return user;
}
}
8. 建数据库
t_user ,id ,name两个字段即可。
9. 测试代码(App.java)
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//标记为springboot应用
public class App {
public static void main(String[] args) {
//启动应用
SpringApplication.run(App.class,args);
}
}