1 package com.oukele.jdbcTemplate;
2
3 import com.mchange.v2.c3p0.ComboPooledDataSource;
4 import org.springframework.context.annotation.Bean;
5 import org.springframework.context.annotation.ComponentScan;
6 import org.springframework.context.annotation.Configuration;
7 import org.springframework.context.annotation.PropertySource;
8 import org.springframework.core.env.Environment;
9 import org.springframework.jdbc.core.JdbcTemplate;
10 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
11 import org.springframework.transaction.PlatformTransactionManager;
12 import org.springframework.transaction.support.TransactionTemplate;
13
14 import javax.sql.DataSource;
15 import java.beans.PropertyVetoException;
16
17 @Configuration
18 @PropertySource(value = "classpath:jdbc.properties")//加载资源
19 @ComponentScan(basePackages = "com.oukele.jdbcTemplate")//扫描 spring 注解
20 public class JDBCTemplateConfig {
21
22 //数据源
23 @Bean
24 DataSource dataSource(Environment env) throws PropertyVetoException {
25 ComboPooledDataSource dataSource = new ComboPooledDataSource();
26 dataSource.setDriverClass(env.getProperty("jdbc.driver"));
27 dataSource.setJdbcUrl(env.getProperty("jdbc.url"));
28 dataSource.setUser(env.getProperty("jdbc.user"));
29 dataSource.setPassword(env.getProperty("jdbc.password"));
30 return dataSource;
31 }
32 //jdbc 模板
33 @Bean
34 JdbcTemplate jdbcTemplate (DataSource dataSource){
35 return new JdbcTemplate(dataSource);
36 }
37 //事务管理器
38 @Bean
39 DataSourceTransactionManager transactionManager(DataSource dataSource){//事务管理
40 return new DataSourceTransactionManager(dataSource);
41 }
42 //事务模板
43 @Bean
44 TransactionTemplate transactionTemplate(PlatformTransactionManager platformTransactionManager){
45 return new TransactionTemplate(platformTransactionManager);
46 }
47
48
49
50
51
52 }
package com.oukele.jdbcTemplate;
import com.oukele.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionTemplate;
import java.util.List;
import java.util.Map;
@Repository
public class UserDao {
@Autowired
private JdbcOperations jdbcOperations;
@Autowired
private TransactionTemplate transactionTemplate;
public List<Map<String, Object>> getToList () {
List<Map<String, Object>> maps = jdbcOperations.queryForList("select * from user");
return maps;
}
public Map<String, Object> getToMap () {
String sql = "select * from user where userName = ?";
Map<String, Object> ret = jdbcOperations.queryForMap(sql, "oukele1");
return ret;
}
public User getToUser () {
String sql = "select * from user where userName = \'oukele\'";
User user = jdbcOperations.queryForObject(
sql,
(rs, rowNum) -> new User(rs.getString(1),rs.getString(2))
);
return user;
}
public int create(String userName, String password) {
transactionTemplate.execute(status ->{
String sql = "insert into user (userName, password) values (?, ?)";
String sq2 = "insert into user (userName, passord) values (?, ?)";
jdbcOperations.update(sq2,userName,password);
return jdbcOperations.update(sql, userName, password);
});
return 0;
}
}
1 package com.oukele.jdbcTemplate;
2
3 import com.oukele.entity.User;
4 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5
6 public class Main {
7 public static void main(String[] args) {
8 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JDBCTemplateConfig.class);
9 UserDao dao = context.getBean(UserDao.class);
10
11 System.out.println("------------------------- List -------------------------");
12 System.out.println(dao.getToList());
13 System.out.println("-------------------------键值对应-------------------------");
14 System.out.println(dao.getToMap());
15 System.out.println();
16 User user = dao.getToUser();
17 System.out.println(user);
18 System.out.println("-------------------------添加-------------------------");
19 System.out.println(dao.create("JAVA", "123"));
20
21 }
22 }
1 jdbc.driver=org.mariadb.jdbc.Driver
2 jdbc.url=jdbc:mariadb://localhost:3306/test
3 jdbc.user=oukele
4 jdbc.password=oukele