當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring模板对象
生活随笔
收集整理的這篇文章主要介紹了
Spring模板对象
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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)使用不同策略的對象實現不同的行為方式,策略對象的變化導致行為的變化。
總結
以上是生活随笔為你收集整理的Spring模板对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pcm 降采样_Android_andr
- 下一篇: c#语言规范所在文件夹,C#规范整理·语