This article describes the use of a spring-boot and the jdbc mysql example. spring-boot configuration file to go really brought a lot of convenience to the developer, from the beginning of this article, we describe the use cases of several spring-boot.
Project dependencies jar package:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
Database using mysql, orm use jdbc implementation.
Here is application.properties configuration:
spring.datasource.url=jdbc\:mysql\://localhost\:3306/zhujinjun?useUnicode\=true&characterEncoding\=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Database (zhujinjun) has a table zh_user
CREATE TABLE `zh_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(64) COLLATE utf8_bin NOT NULL,
`password` varchar(64) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
The following is an abstract class Dao,
public abstract class AbstractJdbcDaoSupport {
@Autowired
protected JdbcTemplate jdbcTemplate;
}
Dao class user table,
@Repository
public class UserRepository extends AbstractJdbcDaoSupport{
private String querySql = "SELECT r.password FROM zh_user r WHERE r.username=?";
private String insertSql = "insert into zh_user(username,password) values(?,?)";
public String getPassword(final String username) {
String passwd = jdbcTemplate.query(querySql, new Object[] { username }, new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet rs) throws SQLException, DataAccessException {
while (rs.next()) {
return rs.getString("password");
}
return null;
}
});
return passwd;
}
public void saveUser(final User user) {
jdbcTemplate.update(insertSql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
}
});
}
}
Here you can achieve access to the database, so the service is no longer like to, I can get from my github account.
Controller class:
@Controller
public class UserController {
@Resource
private MyService myService;
@RequestMapping("/{username}")
@ResponseBody
public String getPassword(@PathVariable String username) {
String passwd = myService.getPasswd(username);
return passwd;
}
@RequestMapping("/saveUser/{username}")
@ResponseBody
public String saveUser(@PathVariable String username) {
User user = new User();
user.setUsername(username);
user.setPassword("111111");
try {
myService.saveUser(user);
return "success!";
} catch (Exception e) {
return "failure!";
}
}
}
Finally, spring-boot startup class App.java
@EnableAutoConfiguration
@ComponentScan("boot.app,boot.service,boot.dao,boot.domain")
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
This completes a spring-boot the demo.
Test Methods:
1. Create a good database
2. Start App.Java class
3. The data input browser address http: // localhost: 8080 / saveUser / abc, successfully inserted into a user abc, return success!
4. browser data input address http: // localhost: 8080 / abc, inserted into the user's password 111111
Source: https: //github.com/jinjunzhu/spring-boot-jdbc.git