Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e88fb72

Browse files
Spring jdbc 操作数据库
1 parent 4cddeac commit e88fb72

File tree

13 files changed

+686
-1
lines changed

13 files changed

+686
-1
lines changed

java/spring/tutorial-spring/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,48 @@
8181
</dependency>
8282

8383

84+
<!-- MySQL 连接配置 -->
85+
<dependency>
86+
<groupId>mysql</groupId>
87+
<artifactId>mysql-connector-java</artifactId>
88+
<version>6.0.6</version>
89+
</dependency>
90+
<dependency>
91+
<groupId>com.mchange</groupId>
92+
<artifactId>c3p0</artifactId>
93+
<version>0.9.5.2</version>
94+
</dependency>
95+
96+
<!-- JdbcTemplate -->
97+
<dependency>
98+
<groupId>org.springframework</groupId>
99+
<artifactId>spring-jdbc</artifactId>
100+
<version>4.3.8.RELEASE</version>
101+
</dependency>
102+
103+
<!-- log4j -->
104+
<dependency>
105+
<groupId>ch.qos.logback</groupId>
106+
<artifactId>logback-classic</artifactId>
107+
<version>1.2.3</version>
108+
<scope>test</scope>
109+
</dependency>
110+
111+
<!-- webSocket -->
112+
<dependency>
113+
<groupId>org.springframework</groupId>
114+
<artifactId>spring-websocket</artifactId>
115+
<version>4.3.9.RELEASE</version>
116+
</dependency>
117+
118+
<!-- sockJS -->
119+
<dependency>
120+
<groupId>com.fasterxml.jackson.core</groupId>
121+
<artifactId>jackson-databind</artifactId>
122+
<version>2.5.3</version>
123+
<scope>runtime</scope>
124+
</dependency>
125+
84126

85127
</dependencies>
86128

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package spring.tutorial.jdbc;
2+
3+
import java.beans.PropertyVetoException;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.jdbc.core.JdbcOperations;
12+
import org.springframework.jdbc.core.JdbcTemplate;
13+
import org.springframework.jndi.JndiObjectFactoryBean;
14+
15+
import com.mchange.v2.c3p0.ComboPooledDataSource;
16+
17+
import spring.tutorial.util.Constants;
18+
19+
@Configuration
20+
@ComponentScan
21+
public class DataSourceConfig {
22+
23+
@Bean
24+
// @Qualifier(Constants.BEAN_ID_DATA_SOURCE_PROPERTY)
25+
public DataSource dataSource() {
26+
ComboPooledDataSource dataSource = new ComboPooledDataSource();
27+
try {
28+
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
29+
dataSource.setJdbcUrl("jdbc:mysql://localhost/data?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC");
30+
dataSource.setUser("root");
31+
dataSource.setPassword("admino0o0oo0");
32+
} catch (PropertyVetoException e) {
33+
e.printStackTrace();
34+
}
35+
return dataSource;
36+
}
37+
38+
@Bean
39+
public JdbcOperations jdbcTemplate(DataSource dataSource) {
40+
return new JdbcTemplate(dataSource);
41+
}
42+
43+
// @Bean
44+
// public JndiObjectFactoryBean jdniObjectFactoryBean() {
45+
// JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
46+
// jndiObjectFactoryBean.setJndiName("jndi/test");
47+
// jndiObjectFactoryBean.setResourceRef(true);
48+
// jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
49+
// return jndiObjectFactoryBean;
50+
// }
51+
//
52+
// @Bean
53+
// @Qualifier(Constants.BEAN_ID_DATA_SOURCE_JNDI)
54+
// public DataSource jndiDataSource(JndiObjectFactoryBean factoryBean) {
55+
// String driverClassName = factoryBean.getJndiEnvironment().getProperty("driverClassName");
56+
// String url = factoryBean.getJndiEnvironment().getProperty("url");
57+
// String user = factoryBean.getJndiEnvironment().getProperty("username");
58+
// String password = factoryBean.getJndiEnvironment().getProperty("password");
59+
// ComboPooledDataSource dataSource = new ComboPooledDataSource();
60+
// try {
61+
// dataSource.setDriverClass(driverClassName);
62+
// dataSource.setJdbcUrl(url);
63+
// dataSource.setUser(user);
64+
// dataSource.setPassword(password);
65+
// } catch (PropertyVetoException e) {
66+
// e.printStackTrace();
67+
// }
68+
// return dataSource;
69+
// }
70+
71+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package spring.tutorial.jdbc.dao;
2+
3+
import java.util.Date;
4+
5+
public class User {
6+
7+
private Integer id;
8+
9+
private String mail;
10+
11+
private String password;
12+
13+
private String salt;
14+
15+
private Date createTime;
16+
17+
private Date updateTime;
18+
19+
public Integer getId() {
20+
return id;
21+
}
22+
23+
public void setId(Integer id) {
24+
this.id = id;
25+
}
26+
27+
public String getMail() {
28+
return mail;
29+
}
30+
31+
public void setMail(String mail) {
32+
this.mail = mail;
33+
}
34+
35+
public String getPassword() {
36+
return password;
37+
}
38+
39+
public void setPassword(String password) {
40+
this.password = password;
41+
}
42+
43+
public String getSalt() {
44+
return salt;
45+
}
46+
47+
public void setSalt(String salt) {
48+
this.salt = salt;
49+
}
50+
51+
public Date getCreateTime() {
52+
return createTime;
53+
}
54+
55+
public void setCreateTime(Date createTime) {
56+
this.createTime = createTime;
57+
}
58+
59+
public Date getUpdateTime() {
60+
return updateTime;
61+
}
62+
63+
public void setUpdateTime(Date updateTime) {
64+
this.updateTime = updateTime;
65+
}
66+
67+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package spring.tutorial.jdbc.dao.repository;
2+
3+
import java.util.List;
4+
5+
import spring.tutorial.jdbc.dao.User;
6+
7+
public interface IUserRepository {
8+
9+
10+
List<User> getUserList(Integer userId);
11+
12+
User getUserDetail(Integer userId);
13+
14+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package spring.tutorial.jdbc.dao.repository.impl;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.jdbc.core.JdbcOperations;
10+
import org.springframework.jdbc.core.RowMapper;
11+
import org.springframework.stereotype.Repository;
12+
13+
import spring.tutorial.jdbc.dao.User;
14+
import spring.tutorial.jdbc.dao.repository.IUserRepository;
15+
16+
@Repository
17+
public class UserRepositoryImpl implements IUserRepository {
18+
19+
@Autowired
20+
private JdbcOperations jdbcOperations;
21+
22+
/**
23+
* 查询用户列表
24+
*
25+
* @author hkf
26+
*/
27+
@Override
28+
public List<User> getUserList(Integer userId) {
29+
List<User> users = new ArrayList<User>();
30+
31+
String sql = "select * from user where id > ?";
32+
users = jdbcOperations.query(sql, new RowMapper<User>() {
33+
34+
@Override
35+
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
36+
if (rs != null) {
37+
User user = new User();
38+
user.setId(rs.getInt("id"));
39+
user.setMail(rs.getString("mail"));
40+
return user;
41+
}
42+
return null;
43+
}
44+
45+
}, new Object[]{userId});
46+
47+
48+
return users;
49+
}
50+
51+
/**
52+
* 查询用户详情
53+
*
54+
* @author hkf
55+
*/
56+
@Override
57+
public User getUserDetail(Integer userId) {
58+
String sql = "select * from user where id = ?";
59+
User user = jdbcOperations.queryForObject(sql, new RowMapper<User>() {
60+
61+
@Override
62+
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
63+
if (rs != null) {
64+
User user = new User();
65+
user.setId(rs.getInt("id"));
66+
user.setMail(rs.getString("mail"));
67+
return user;
68+
}
69+
return null;
70+
}
71+
72+
}, new Object[]{userId});
73+
74+
return user;
75+
}
76+
77+
}

java/spring/tutorial-spring/src/main/java/spring/tutorial/util/Constants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ public class Constants {
2222
user_type_x_permission.put("user", "");
2323
}
2424

25+
public static final String BEAN_ID_DATA_SOURCE_PROPERTY = "dataSourceOfProperty";
26+
27+
public static final String BEAN_ID_DATA_SOURCE_JNDI = "dataSourceOfJndi";
28+
2529
}

java/spring/tutorial-spring/src/main/java/spring/tutorial/web/base/RootConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.ComponentScan;
77
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.Import;
89
import org.springframework.core.io.FileSystemResource;
910
import org.springframework.web.multipart.MultipartResolver;
1011
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
1112

13+
import spring.tutorial.jdbc.DataSourceConfig;
14+
import spring.tutorial.websocket.WebSocketConfig;
15+
1216
@Configuration
1317
@ComponentScan
18+
@Import({DataSourceConfig.class, WebSocketConfig.class})
1419
public class RootConfig {
1520

1621
// @Bean

java/spring/tutorial-spring/src/main/java/spring/tutorial/web/base/controller/UserController.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.util.List;
56

7+
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.stereotype.Controller;
79
import org.springframework.web.bind.annotation.RequestMapping;
810
import org.springframework.web.bind.annotation.RequestMethod;
@@ -11,17 +13,23 @@
1113
import org.springframework.web.bind.annotation.ResponseBody;
1214
import org.springframework.web.multipart.MultipartFile;
1315

16+
import spring.tutorial.jdbc.dao.User;
17+
import spring.tutorial.jdbc.dao.repository.IUserRepository;
1418
import spring.tutorial.web.base.exception.TutorialException;
1519
import spring.tutorial.web.base.vo.UserVO;
1620

1721
@Controller
1822
@RequestMapping(value="/test")
1923
public class UserController {
2024

25+
@Autowired
26+
private IUserRepository iUserRepository;
27+
2128
@ResponseBody
2229
@RequestMapping(value={"/", "/hello"}, method=RequestMethod.GET)
2330
public String sayHello() {
24-
return "say hello";
31+
System.out.println("This is test");
32+
return "say hello123s";
2533
}
2634

2735
@RequestMapping(value="/helloWorld", method=RequestMethod.GET)
@@ -64,4 +72,11 @@ public String uploadFile3(UserVO requestVO) {
6472
public String handleException(){
6573
throw new TutorialException("this is test");
6674
}
75+
76+
@ResponseBody
77+
@RequestMapping(value="/getUserList")
78+
public String getUserList() {
79+
List<User> users = iUserRepository.getUserList(1000);
80+
return null;
81+
}
6782
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package spring.tutorial.websocket;
2+
3+
import org.springframework.web.socket.TextMessage;
4+
import org.springframework.web.socket.WebSocketSession;
5+
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
6+
7+
public class MarcoHandler extends AbstractWebSocketHandler {
8+
9+
10+
@Override
11+
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
12+
13+
System.out.println("received message:" + message.getPayload());
14+
15+
Thread.sleep(2000);
16+
17+
session.sendMessage(new TextMessage("Polo!"));
18+
}
19+
}

0 commit comments

Comments
 (0)