发表于: 2017-10-02 20:10:12

1 813


今天完成的事情:

上午尝试学习jdbcTemplate,写了个例子,对jdbcTemplate有了一定的了解

下面是配置jdbc的思路

1. 我要有DataSource,DataSource的属性可以通过注入数据库的一些配置属性添加

2. 我要有JdbcTemplate,而Template依赖与DataSource,我要以ref的方式为我的JdbcTemplate注入引用

3. 有了JdbcTemplate之后,我要有Dao,此时我应该在Dao添加一个JdbcTemplate的成员,然后以ref的方式将JdbcTemplate引入到Dao中

4. 我在Action或者是Servlet中都会调用的是Serivce,所以,我在Serivce中要添加一个Dao作为成员,然后由ref在注入Dao到Service中

DataSource --> JdbcTemplate --> Dao --> Service --> Action/Servlet

"-->"表示将左边的对象注入到右边的对象当中

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring_03"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userDao" class="com.spring.dao.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="userService" class="com.spring.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>

这是大概配置

接下来就是jdbcTemplate的一些基础的方法

public static void main(String[] args) {
//curd代码
   User user = new User();
   user.setId(1);
   user = query(1);
   if (user ==null){
System.out.println("查询失败");
   }else {
System.out.println(user);
       System.out.println("查询成功");
   }
}
public static void insert(User user){
String sql = "insert into user(name,birthday,money) values(?,?,?)";
   Object args[] = {user.getName(),user.getBirthday(),user.getMoney()};
   int temp = jdbc.update(sql,args);
   if (temp>0){
System.out.println("插入成功");
   }else {
System.out.println("插入失败");
   }
}
public static void delete(int id) {
String sql = "delete from user where id = ?";
   Object args[] = new Object[]{id};
   int temp = jdbc.update(sql,args);
   if (temp>0){
System.out.println("更新成功");
   }else {
System.out.println("更新失败");
   }
}
public static User query(int id) {
String sql = "select * from user where id = ?";
   Object args[] = new Object[]{id};
   Object user = jdbc.queryForObject(sql,args,new BeanPropertyRowMapper(User.class));
   return (User)user;
}

然后根据博涛的小课堂回顾学习了一波spring aop面向切面编程,把之前的概念转化成了实实在在的代码终于得以理解什么叫面向切面

这是配置文件

<tx:advice id="txAdvice" transaction-manager="txManager">
   <tx:attributes>
       <!-- <tx:method> 给切入点添加事务详情
            name:方法名称, *表示任意方法, do* 表示以do开头的方法
            propagation:设置传播行为
            isolation:隔离级别
            read-only:是否只读 -->
       <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
       <!--ISOLATION_DEFAULT 默认级别(对大多数数据库来说就是ISOLATION_READ_COMMITTED-->
       <!--ISOLATION_READ_UNCOMMITTED 最低的隔离级别。事实上我们不应该隔离级别,因为在事务完成前,其他事务可以看到该事务所修改的数据。
       而在其他事务提交前,该事务也可以看到其他事务所做的修改。-->
       <!--ISOLATION_READ_COMMITTED 大多数数据库的默认级别。在事务完成前,其他事务无法看到该事务所修改的数据。
       遗憾的是,在该事务提交后,你就可以查看其他事务插入活更新的数据。这意味着在事务的不同点上,如果其他事务修改数据,你会看到不同的数据。-->
       <!--ISOLATION_REPEATABLE_READ 该隔离级别确保如果在事务中查询了某个数据集,你至少还能再次查询到相同的数据集,即使其他事务修改了所查询的数据。
       然而如果其他事务插入了新数据,你就可以查询到该新插入的数据。-->
       <!--ISOLATION_SERIALIZABLE 代价最大、可靠性最高的隔离级别,所有的事务都是俺顺序一个接一个的执行。-->
   </tx:attributes>
</tx:advice>
<aop:config>
   <!-- 切入点 -->
   <aop:pointcut expression="execution(* ServiceImpl.AccountServiceImpl.*(..))" id="txPointCut"/>
   <!-- 切面:将切入点和通知整合 -->
   <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<bean id="logHandler" class="Util.LogHandler"/>
<aop:config>
   <aop:aspect id="log" ref="logHandler" order="1">
       <aop:pointcut id="logTime" expression="execution(* ServiceImpl.AccountServiceImpl.transfer(..))"/>
       <aop:before method="LogBefore" pointcut-ref="logTime"/>
       <aop:after method="LogAfter" pointcut-ref="logTime"/>
       <aop:around method="around" pointcut-ref="logTime"/>
   </aop:aspect>
</aop:config>

明天计划的事情:

继续改项目

遇到的问题:

就是关于jdbcTemplate的有一个地方不是很清楚

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

dataSource的class有的是这个,还有的是另一个

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

就有点不是很明白

收获:

学习到了不少东西


返回列表 返回列表
评论

    分享到