Spring系列介绍3

简介:

public  class Person {
   private Integer id;
   private String name;
  
   public Person(){}
  
   public Person(String name) {
     this.name = name;
  }
   public Integer getId() {
     return id;
  }
   public  void setId(Integer id) {
     this.id = id;
  }
   public String getName() {
     return name;
  }
   public  void setName(String name) {
     this.name = name;
  }
}
 
public  class PersonRowMapper  implements RowMapper {

   public Object mapRow(ResultSet rs,  int index)  throws SQLException {
    Person person =  new Person(rs.getString( "name"));
    person.setId(rs.getInt( "id"));
     return person;
  }
}
public  interface PersonService {
   /**
    * 保存person
    * @param person
    */

   public  void save(Person person);
   /**
    * 更新person
    * @param person
    */

   public  void update(Person person);
   /**
    * 获取person
    * @param personid
    * @return
    */

   public Person getPerson(Integer personid);
   /**
    * 获取所有person
    * @return
    */

   public List<Person> getPersons();
   /**
    * 删除指定id的person
    * @param personid
    */

   public  void delete(Integer personid);
}
 
@Transactional
public  class PersonServiceBean  implements PersonService {
   private JdbcTemplate jdbcTemplate;
  
   public  void setDataSource(DataSource dataSource) {
     this.jdbcTemplate =  new JdbcTemplate(dataSource);
  }

   public  void delete(Integer personid) {
    jdbcTemplate.update( "delete from person where id=?"new Object[]{personid},
         new  int[]{java.sql.Types.INTEGER});
  }

   public Person getPerson(Integer personid) {    
     return (Person)jdbcTemplate.queryForObject( "select * from person where id=?"new Object[]{personid}, 
         new  int[]{java.sql.Types.INTEGER},  new PersonRowMapper());
  }

  @SuppressWarnings( "unchecked")
   public List<Person> getPersons() {
     return (List<Person>)jdbcTemplate.query( "select * from person"new PersonRowMapper());
  }

   public  void save(Person person) {
    jdbcTemplate.update( "insert into person(name) values(?)"new Object[]{person.getName()},
         new  int[]{java.sql.Types.VARCHAR});
  }

   public  void update(Person person) {
    jdbcTemplate.update( "update person set name=? where id=?"new Object[]{person.getName(), person.getId()},
         new  int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
  }
}
 
jdbc.properties
driverClassName=org.gjt.mm.mysql.Driver
url=jdbc\:mysql\://localhost\:3306/itcast?useUnicode\=true&characterEncoding\=UTF-8
username=root
password=123456
initialSize=1
maxActive=500
maxIdle=2
minIdle=1
<? 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
                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >

     < context:property-placeholder  location ="classpath:jdbc.properties" />
     < bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource"  destroy-method ="close" >
          < property  name ="driverClassName"  value ="${driverClassName}" />
          < property  name ="url"  value ="${url}" />
          < property  name ="username"  value ="${username}" />
          < property  name ="password"  value ="${password}" />
            <!--  连接池启动时的初始值 -->
      < property  name ="initialSize"  value ="${initialSize}" />
     <!--  连接池的最大值 -->
      < property  name ="maxActive"  value ="${maxActive}" />
     <!--  最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
      < property  name ="maxIdle"  value ="${maxIdle}" />
     <!--     最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
      < property  name ="minIdle"  value ="${minIdle}" />
     </ bean >

   < bean  id ="txManager"  class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
             < property  name ="dataSource"  ref ="dataSource" />
         </ bean >
   < tx:annotation-driven  transaction-manager ="txManager" />
  
   < bean  id ="personService"  class ="cn.itcast.service.impl.PersonServiceBean" >
     < property  name ="dataSource"  ref ="dataSource" />
   </ bean >
</ beans >
 
 

本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/273740,如需转载请自行联系原作者 
相关文章
|
24天前
|
缓存 Java Maven
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
|
2月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
2月前
|
Java 应用服务中间件 开发者
Java面试题:解释Spring Boot的优势及其自动配置原理
Java面试题:解释Spring Boot的优势及其自动配置原理
95 0
|
16天前
|
缓存 Java 数据库连接
Spring Boot 资源文件属性配置,紧跟技术热点,为你的应用注入灵动活力!
【8月更文挑战第29天】在Spring Boot开发中,资源文件属性配置至关重要,它让开发者能灵活定制应用行为而不改动代码,极大提升了可维护性和扩展性。Spring Boot支持多种配置文件类型,如`application.properties`和`application.yml`,分别位于项目的resources目录下。`.properties`文件采用键值对形式,而`yml`文件则具有更清晰的层次结构,适合复杂配置。此外,Spring Boot还支持占位符引用和其他外部来源的属性值,便于不同环境下覆盖默认配置。通过合理配置,应用能快速适应各种环境与需求变化。
26 0
|
1月前
|
XML Java 数据库连接
Spring Boot集成MyBatis
主要系统的讲解了 Spring Boot 集成 MyBatis 的过程,分为基于 xml 形式和基于注解的形式来讲解,通过实际配置手把手讲解了 Spring Boot 中 MyBatis 的使用方式,并针对注解方式,讲解了常见的问题已经解决方式,有很强的实战意义。在实际项目中,建议根据实际情况来确定使用哪种方式,一般 xml 和注解都在用。
|
2月前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
79 11
|
24天前
|
缓存 Java Spring
Java本地高性能缓存实践问题之在Spring Boot中启用缓存支持的问题如何解决
Java本地高性能缓存实践问题之在Spring Boot中启用缓存支持的问题如何解决
|
2月前
|
Java 开发者 Spring
深入理解Spring Boot中的自动配置原理
深入理解Spring Boot中的自动配置原理