the third day of spring and spring jdbcTemplate Management Services

1. Based on aspectj comment aop (will be used)

  • Create objects in bean.xml
    Here Insert Picture Description
  • Aop bean.xml opening operation in
    the image insertion described herein
  • Use aop enhanced annotation above the class: @Aspect
  • In the above method of enhancing the reinforcing type configuration: @Before (value = "entry point") and the like
    - for example:
    Here Insert Picture Description
    Here Insert Picture Description

2.Spring operation of jdbcTemplate

2.1 spring frame stop frame

Uses : spring for javaee three layers, each layer has to solve technology. For dao layer, jdbcTemplate class.
Achieved : spring persistence layer for different technologies encapsulated
Here Insert Picture Description

2.2 jdbcTemplate

Jdbc fact is encapsulated, and the use of the jdbcTemplate dbutils use very similar, all database operations crud.

(0) leader packet: Import jdbcTemplate jar package used, as well as database-driven package
Here Insert Picture Description
actually check the following additions and deletions can be modified to create an object configuration file is implemented.
(1) by
creating a data source object, set the database information
to create jdbcTemplate objects, set the data source
to call the object inside jdbcTemplate update(sql,args..)way to achieve operation
to add, delete, modify, update methods are used .
Here Insert Picture Description
Here Insert Picture Description
(2) delete
Here Insert Picture Description
(3) change
Here Insert Picture Description
(4) check:
comprising the following three cases in which the latter two cases need to write their own class implements the interface for data encapsulation RowMapper

  • 第一种 查询返回某一个值Here Insert Picture Description
    Here Insert Picture Description
  • 第二种 查询返回对象Here Insert Picture Description
    Here Insert Picture Description
  • 第三种 查询返回list集合Here Insert Picture DescriptionHere Insert Picture Description

实现类MyRowMapper :
用于从结果集获取数据

class MyRowMapper implements RowMapper<User> {

	@Override
	public User mapRow(ResultSet rs, int num) throws SQLException {
		// 1 从结果集里面把数据得到
		String username = rs.getString("username");
		String password = rs.getString("password");
		
		// 2 把得到数据封装到对象里面
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		
		return user;
	}
	
}

附:jdbc实现代码:

	//2 jdbc实现代码
	@Test
	public void testJDBC() {
		Connection conn = null;
		PreparedStatement psmt = null;
		ResultSet rs = null;
		//加载驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
			//创建连接
			conn = DriverManager.getConnection("jdbc:mysql:///spring_day03", "root", "root");
			//编写sql语句
			String sql = "select * from user where username=?";
			//预编译sql
			psmt = conn.prepareStatement(sql);
			//设置参数值
			psmt.setString(1, "lucy");
			//执行sql
			rs = psmt.executeQuery();
			//遍历结果集
			while(rs.next()) {
				//得到返回结果值
				String username = rs.getString("username");
				String password = rs.getString("password");
				//放到user对象里面
				User user = new User();
				user.setUsername(username);
				user.setPassword(password);
				
				System.out.println(user);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				psmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

2.3 Spring配置连接池

(1)导入jar包:
Here Insert Picture Description
(2)创建spring配置文件,配置连接池
原理:
Here Insert Picture Description
配置实现:
Here Insert Picture Description

2.4 dao使用jdbcTemplate

功能:在数据库中插入数据
思路:基于xml配置所有对象,包括dataSource对象(配置数据库连接参数)、jdbcTemplate对象(用于和数据库交互,需要dataSource)、dao对象(提供具体操作,需要jdbcTemplate)、service对象(调用dao对象的操作,需要dao)
步骤
配置c3p0连接池dataSource
配置jdbcTemplate对象,把dataSource注入到jdbcTemplate里面
配置dao对象,把jdbcTemplate注入到dao里面
配置service对象,把dao对象注入到service里面

<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 注入属性值 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///spring_day03"></property>
		<property name="user" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	
	<!-- 创建jdbcTemplate对象 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 把dataSource传递到模板对象里面 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 创建service和dao对象,在service注入dao对象 -->
	<bean id="userService" class="cn.itcast.c3p0.UserService">
		<!-- 注入dao对象 -->
		<property name="userDao" ref="userDao"></property>
	</bean>
	
	<bean id="userDao" class="cn.itcast.c3p0.UserDao">
		<!-- 注入jdbcTemplate对象 -->
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>

3. Spring的事务管理

(1)声明式事务管理(XML)
配置文件方式使用aop思想配置

  • 配置数据库事务管理器:需要数据库参数
  • 配置事务增强:指定切入点方法的匹配规则
  • 配置切面:指定增强用于哪个切入点
    Here Insert Picture Description

(2)声明式事务管理(注解)

  • 配置数据库事务管理器:需要数据库参数
  • 开启事务注解:指定事务管理器
  • 事务方法所在的类上方添加注解@Transaction
    Here Insert Picture Description
    Here Insert Picture Description
Published 135 original articles · won praise 5 · Views 7084

Guess you like

Origin blog.csdn.net/qq_27921205/article/details/104573947