Spring模块对象:
把共性的方法抽取出来固定为一个模板,后续再操作只需要填充内容即可。
比如:淘宝每次买东西都要填写地址,只是每次买的东西不一样,所以可以做一个默认地址,每次买东西都要去选商品就行了,不需要每次都填写地址。
JdbcTemplate
pom.xml
<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>
工具类JDBCConfig
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);}
}
工具类SpringConfig
@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import(JDBCConfig.class)
public class SpringConfig {
}
AccountDao
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();
}
实现类AccountDaoImpl:(提供标准的sql语句操作API)
//dao注册为bean
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {//注入模板对象@Autowiredprivate 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);}
}
实现类AccountDaoImpl2:
//dao注册为bean
@Repository
//@Primary
public class AccountDaoImpl2 implements AccountDao {//注入模板对象@Autowiredprivate 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;}
}
实体类Account
public class Account implements Serializable {private Integer id;private String name;private Double money;
}
AccountService
public interface AccountService {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();
}
AccountServiceImpl
@Service("accountService")
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;public void save(Account account) {accountDao.save(account);}public void delete(Integer id) {accountDao.delete(id);}public void update(Account account){accountDao.update(account);}public String findNameById(Integer id) {return accountDao.findNameById(id);}public Account findById(Integer id) {return accountDao.findById(id);}public List<Account> findAll() {return accountDao.findAll();}public List<Account> findAll(int pageNum, int preNum) {return accountDao.findAll(pageNum,preNum);}public Long getCount() {return accountDao.getCount();}
}
pom.xml
<dependencies><!--无mybatis配置坐标--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.9.RELEASE</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><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></dependencies>
RedisTemplate
RedisTemplate对象结构
public void changeMoney(Integer id, Double money) {redisTemplate.opsForValue().set("account:id:"+id,money);
}
public Double findMondyById(Integer id) {Object money = redisTemplate.opsForValue().get("account:id:" + id);return new Double(money.toString());
}
策略模式应用:
策略模式(Strategy Pattern)使用不同策略的对象实现不同的行为方式,策略对象的变化导致行为的变化。