Spring學習筆記——第三天

一、Spring的jdbcTemplate操作

1.導入所需jar包

2.設置數據庫信息

3.創建jdbcTemplate對象

4.調用方法

  • 添加操作
        @Test
	public void add(){
		//設置數據庫信息
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/spring?serverTimezone=UTC");
		dataSource.setUsername("root");
		dataSource.setPassword("123");
		//創建jdbcTemplate對象,設置數據源
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		//調用方法實現
		//編寫sql語句
		String sql = "insert into user values(?,?)";
		jdbcTemplate.update(sql, "baozi", "250");
	}
  • 修改操作
        @Test
	public void update(){
                //將之前的數據庫信息設置封裝成了一個方法
		DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		String sql = "update user set password = ? where username = ?";
		jdbcTemplate.update(sql, "sb" , "baozi");
	}
  • 刪除操作
	@Test
	public void delete(){
		DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		String sql = "delete from user where username = ?";
		jdbcTemplate.update(sql, "baozi");
	}
  • 查詢操作

(1)查詢返回某一個值

	@Test
	public void select1(){
		DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		String sql = "select count(*) from user";
                //返回一個值用queryForObject方法,第一個參數爲sql語句,第二個參數爲返回值類型的class
		int count = jdbcTemplate.queryForObject(sql, Integer.class);
		System.out.println(count);
	}

(2)查詢返回對象

queryForObject方法,第一個參數爲sql語句,第二個參數爲RowMapper接口,需要自己寫類實現接口,第三個參數是查詢參數

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;
	}
}
	@Test
	public void select2(){
		DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		String sql = "select *  from user where username = ?";
		User user = jdbcTemplate.queryForObject(sql, new MyRowMapper(), "孫笑川");
		System.out.println(user);
	}

 (3)返回list集合

	@Test
	public void select3(){
		DriverManagerDataSource dataSource = JdbcUtils.getDataSource();
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		String sql = "select * from user";
		List<User> list = jdbcTemplate.query(sql, new MyRowMapper());
		System.out.println(list);
	}

二、Spring配置連接池

1.導入jar包

2.配置連接池

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=UTC"></property>
	<property name="user" value="root"></property>
	<property name="password" value="123"></property>
</bean>

三、在dao中使用jdbcTemplate

1.創建service對象,在service中注入dao

public class UserService {
	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void add(){
		userDao.add();
	}
}

2.在dao對象中注入jdbcTemplate

public class UserDao {
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void add(){
		String sql = "insert into user values(?,?)";
		jdbcTemplate.update(sql, "嗯掃", "eight");
	}
}

3.在jdbcTemplate中注入dataSource 

	<bean id="userDao" class="cn.itcast.c3p0.UserDao">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
	<bean id="userService" class="cn.itcast.c3p0.UserService">
		<property name="userDao" ref="userDao"></property>
	</bean>
	
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 把dataSource注入到模板對象中 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?serverTimezone=UTC"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123"></property>
	</bean>

測試代碼

	@Test
	public void testC3p0(){
		ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
		UserService userService = (UserService) context.getBean("userService");
		userService.add();
	}

四、Spring事務管理

1.聲明式事務管理(xml)

	<!-- 1.配置事務管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 2.配置事務增強 -->
	<tx:advice id="txadvice" transaction-manager="transactionManager">
		<!-- 做事務操作 -->
		<tx:attributes>
			<!-- 設置進行事務操作的方法匹配規則,name屬性爲做事務操作的方法名 -->
			<tx:method name="account*"/>
		</tx:attributes>
	</tx:advice>
	<!-- 3.配置切面 -->
	<aop:config>
		<!-- 切入點 -->
		<aop:pointcut expression="execution()" id="pointcut1"/>
		<!-- 切面 -->
		<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
	</aop:config>

2.聲明式事務管理(註解)

	<!-- 1.配置事務管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 2.開啓註解事務 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
        <!-- 3.在需要做事務操作的方法所在的類上使用註解@Transactional

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章