maven坐标
<dependencies>
<!--无mybatis配置坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
jdbc配置类:
public class JDBCConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String userName;
@Value("${jdbc.password}")
private String password;
@Bean("dataSource")
public DataSource getDataSource(){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
}
//注册JdbcTemplate模块对象bean
@Bean("jdbcTemplate")
public JdbcTemplate getJdbcTemplate(@Autowired DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean("jdbcTemplate2")
public NamedParameterJdbcTemplate getJdbcTemplate2(@Autowired DataSource dataSource){
return new NamedParameterJdbcTemplate(dataSource);
}
}
Spring配置文件:
@Configuration
@ComponentScan("com.test")
@PropertySource("classpath:jdbc.properties")
@Import(JDBCConfig.class)
public class SpringConfig {
}
数据库配置文件:忽略
dao层代码:
public interface AccountDao {
void save(Account account);
void delete(Integer id);
void update(Account account);
String findNameById(Integer id);
Account findById(Integer id);
List<Account> findAll();
List<Account> findAll(int pageNum,int preNum);
Long getCount();
}
dao实现:
实现一:
//dao注册为bean
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
//注入模板对象
@Autowired
private JdbcTemplate jdbcTemplate;
public void save(Account account) {
String sql = "insert into account(name,money)values(?,?)";
jdbcTemplate.update(sql,account.getName(),account.getMoney());
}
public void delete(Integer id) {
String sql = "delete from account where id = ?";
jdbcTemplate.update(sql,id);
}
public void update(Account account) {
String sql = "update account set name = ? , money = ? where id = ?";
jdbcTemplate.update(sql, account.getName(),account.getMoney(),account.getId());
}
public String findNameById(Integer id) {
String sql = "select name from account where id = ? ";
//单字段查询可以使用专用的查询方法,必须制定查询出的数据类型,例如name为String类型
return jdbcTemplate.queryForObject(sql,String.class,id );
}
public Account findById(Integer id) {
String sql = "select * from account where id = ? ";
//支持自定义行映射解析器
RowMapper<Account> rm = new RowMapper<Account>() {
public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
return account;
}
};
return jdbcTemplate.queryForObject(sql,rm,id);
}
public List<Account> findAll() {
String sql = "select * from account";
//使用spring自带的行映射解析器,要求必须是标准封装
return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Account>(Account.class));
}
public List<Account> findAll(int pageNum, int preNum) {
String sql = "select * from account limit ?,?";
//分页数据通过查询参数赋值
return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Account>(Account.class),(pageNum-1)*preNum,preNum);
}
public Long getCount() {
String sql = "select count(id) from account ";
//单字段查询可以使用专用的查询方法,必须制定查询出的数据类型,例如数据总量为Long类型
return jdbcTemplate.queryForObject(sql,Long.class);
}
}
实现二:
//dao注册为bean
@Repository
//@Primary
public class AccountDaoImpl2 implements AccountDao {
//注入模板对象
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
public void save(Account account) {
String sql = "insert into account(name,money)values(:name,:money)";
Map pm = new HashMap();
pm.put("name",account.getName());
pm.put("money",account.getMoney());
jdbcTemplate.update(sql,pm);
}
public void delete(Integer id) {
}
public void update(Account account) {
}
public String findNameById(Integer id) {
return null;
}
public Account findById(Integer id) {
return null;
}
public List<Account> findAll() {
return null;
}
public List<Account> findAll(int pageNum, int preNum) {
return null;
}
public Long getCount() {
return null;
}
}
基本用就这样使用了。。。这种用法很少见,几乎都是mybatis操作持久层,这种用法了解一下,,,,,,,!
RedisTemplate模板!
maven坐标:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
template
@PropertySource("redis.properties")
public class RedisConfig {
@Value("${redis.host}")
private String hostName;
@Value("${redis.port}")
private Integer port;
// @Value("${redis.password}")
// private String password;
@Value("${redis.maxActive}")
private Integer maxActive;
@Value("${redis.minIdle}")
private Integer minIdle;
@Value("${redis.maxIdle}")
private Integer maxIdle;
@Value("${redis.maxWait}")
private Integer maxWait;
@Bean
//配置RedisTemplate
public RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory){
//1.创建对象
RedisTemplate redisTemplate = new RedisTemplate();
//2.设置连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
//3.设置redis生成的key的序列化器,对key编码进行处理
RedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
//4.返回
return redisTemplate;
}
@Bean
//配置Redis连接工厂
public RedisConnectionFactory createRedisConnectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration,GenericObjectPoolConfig genericObjectPoolConfig){
//1.创建配置构建器,它是基于池的思想管理Jedis连接的
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder builder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();
//2.设置池的配置信息对象
builder.poolConfig(genericObjectPoolConfig);
//3.创建Jedis连接工厂
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration,builder.build());
//4.返回
return jedisConnectionFactory;
}
@Bean
//配置spring提供的Redis连接池信息
public GenericObjectPoolConfig createGenericObjectPoolConfig(){
//1.创建Jedis连接池的配置对象
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
//2.设置连接池信息
genericObjectPoolConfig.setMaxTotal(maxActive);
genericObjectPoolConfig.setMinIdle(minIdle);
genericObjectPoolConfig.setMaxIdle(maxIdle);
genericObjectPoolConfig.setMaxWaitMillis(maxWait);
//3.返回
return genericObjectPoolConfig;
}
@Bean
//配置Redis标准连接配置对象
public RedisStandaloneConfiguration createRedisStandaloneConfiguration(){
//1.创建Redis服务器配置信息对象
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
//2.设置Redis服务器地址,端口和密码(如果有密码的话)
redisStandaloneConfiguration.setHostName(hostName);
redisStandaloneConfiguration.setPort(port);
// redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
//3.返回
return redisStandaloneConfiguration;
}
}
配置文件:
# redis服务器主机地址 redis.host=127.0.0.1 #redis服务器主机端口 redis.port=6379 #redis服务器登录密码 #redis.password=123 #最大活动连接 redis.maxActive=20 #最大空闲连接 redis.maxIdle=10 #最小空闲连接 redis.minIdle=0 #最大等待时间 redis.maxWait=-
xml配置文件形式的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--引入读取文件的-->
<context:property-placeholder location="classpath:redis2.properties"/>
<!-- redis pool -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!-- redis pool config -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<!--密码配置-->
<!--<property name="password" value="${redis.password}"></property>-->
<property name="poolConfig" ref="poolConfig"></property>
</bean>
<!--redis redisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="enableTransactionSupport" value="true"></property>
</bean>
<!--set注入使用类-->
<bean id="user" class="redis.User">
<property name="redisTemplate" ref="redisTemplate"/>
</bean>
</beans>
其实就是自备的工具类:用到来粘贴修改!