A. Configure Data Source
Whether you choose which of Spring data access, you need to configure a data source references. Spring provides a variety of data sources arranged in the Spring bean context, comprising:
Defined by the JDBC driver data source
through JNDI lookup data source
connection pool data source
1.1 JNDI data source
Use Java configuration, we can help find DataSource JndiObjectFactoryBean in:
@Bean public DataSource productionDataSource(){ //使用JNDI数据源 JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); jndiObjectFactoryBean.setJndiName("org.h2.Driver"); jndiObjectFactoryBean.setResourceRef(true); jndiObjectFactoryBean.setProxyInterface(DataSource.class); return (DataSource) jndiObjectFactoryBean.getObject(); }
1.2 using a data source pool
Use Java configuration, with the statement BasicDataSource bean class as follows:
@Bean public DataSource quDataSource(){ //数据源连接池 BasicDataSource basicDataSource = new BasicDataSource(); basicDataSource.setDriverClassName("org.h2.Driver"); basicDataSource.setUrl("jdbc:h2:tcp://localhost/~/spitter"); basicDataSource.setUsername("sa"); basicDataSource.setPassword(""); return basicDataSource; }
1.3 JDBC-based data source driver
in the Spring, the source data defined by the JDBC driver is the simplest configuration. Spring provides three classes of such data sources for selection :
DriverManagerDataSource: returns a new connection at each connection request. JDBC-BasicDataSource different connection provided by DriverManagerDataSource and no pool management.
SimpleDriverDataSource: DriverManagerDataSource and works like, but it directly using JDBC driver to solve the class loading problems under certain circumstances, such an environment include OSGi container.
SingleConnectionDataSource: returns the same connection at each connection request. Although SingleConnectionDataSource connection pool data source is not in the strict sense, but it can be viewed as only one connection pool.
Note: SingleConnectionDataSource there is one and only one database connection is not suitable for multi-threaded, DriverManagerDataSource and SimpleDriverDataSource Although support multithreading, but creates a new connection on every request, which is based on performance for the price.
Use Java configuration, with the bean declaration DriverManagerDataSource class:
//基于JDBC驱动的数据源 @Bean public DataSource jdbcDataSource(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:tcp://localhost/~/spitter"); dataSource.setUsername("sa"); dataSource.setPassword(""); return dataSource; }
1.4 using embedded data source
Use Java configuration , the aid EmbeddedDatabaseBuilder bean class declaration:
@Bean public the DataSource developmentDataSource () { // embedded original data: each time you restart the application or run tests of time, are able to repopulate the test data return new new EmbeddedDatabaseBuilder () .setType (EmbeddedDatabaseType.H2) .addScript ( "the CLASSPATH: Schema .sql " ) .addScript ( " CLASSPATH: Test-data.sql " ) .build (); }
JdbcTemplate used to read data:
@Component public class DbUserDaoImpl implements DbUserDao { @Autowired private JdbcTemplate jdbcTemplate; private static final String SQL_INSERT_DBUSER = "insert into user_list (username, mobile, cardnumber) values (?, ?, ?)"; private static final String SQL_CHECK_DBUSER = "select id,username,mobile,cardnumber from user_list"; private static final String SQL_FINDBYID_DBUSER = "select id,username,mobile,cardnumber from user_list where id = ?"; private static final String SQL_UPDATE_DBUSER = "update user_list set username = ?,mobile = ?,cardnumber = ? where id = ?"; @Override public void addDbUser(DbUser dbUser) { jdbcTemplate.update(SQL_INSERT_DBUSER, dbUser.getUsername(), dbUser.getMobile(), dbUser.getCardnumber()); } @Override public List<DbUser> findAll() { return jdbcTemplate.query(SQL_CHECK_DBUSER, new DbUserRowMapper()); } @Override public DbUser findById(int id) { return jdbcTemplate.queryForObject(SQL_FINDBYID_DBUSER, new DbUserRowMapper(), id); } public static final class DbUserRowMapper implements RowMapper<DbUser>{ public DbUser mapRow(ResultSet rs, int rowNum) throws SQLException { int id = rs.getInt("id"); String username = rs.getString("username"); String mobile = rs.getString("mobile"); String cardnumber = rs.getString("cardnumber"); DbUser dbUser = new DbUser(); dbUser.setId(id); dbUser.setUsername(username); dbUser.setMobile(mobile); dbUser.setCardnumber(cardnumber); return dbUser; } } public DbUser save(DbUser dbUser){ if(dbUser.getId() == null){ int id = insertDbUserAndReturnId(dbUser); return jdbcTemplate.queryForObject(SQL_FINDBYID_DBUSER, new DbUserRowMapper(), id); } jdbcTemplate.update(SQL_UPDATE_DBUSER, dbUser.getUsername(), dbUser.getMobile(), dbUser.getCardnumber(), dbUser.getId()); return dbUser; } public int insertDbUserAndReturnId(DbUser dbUser){ SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName("user_list"); jdbcInsert.setGeneratedKeyName("id"); Map<String, Object> map = new HashMap<>(); map.put("username", dbUser.getUsername()); map.put("mobile", dbUser.getMobile()); map.put("cardnumber", dbUser.getCardnumber()); int id = jdbcInsert.executeAndReturnKey(map).intValue(); return id; } }
This article comes from: https://blog.csdn.net/qq_22314145/article/details/81562244