spring基础
1.spring注解
1.jar包
2. 配置文件
2. spring AOP切面编程
2.1 jar包
3. spring中的JdbcTemplate
jdbcTemplate是对原始的jdbc做了封装。
3.1 jar包
3.2 基本操作
public void test(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/db_jdbc?characterEncoding=utf-8"); dataSource.setUsername("root"); dataSource.setPassword("123"); JdbcTemplate template = new JdbcTemplate(); //进行操作 template.execute("INSERT INTO tb_user () VALUES ()"); }
3.3 基于注解
bean.xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="cn.getword" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/db_jdbc?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="123" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
UserDaoImpl:
@Repository public class UserDaoImpl implements UserDao { @Autowired private JdbcTemplate jdbcTemplate; public List<User> save(){ List<User> users = new ArrayList<User>(); List<Map<String, Object>> maps = jdbcTemplate.queryForList("SELECT * FROM users "); for (Map<String, Object> map : maps) { User user = new User() {{ setId((Integer) map.get("id")); setUsername((String) map.get("username")); setPassword((String) map.get("password")); setAge((Integer) map.get("age")); setRegisterTime((Date) map.get("registerTime")); }}; users.add(user); } return users; } }
4. spring的声明式事务控制【使用JdbcTemplate】
4.1 jar包
核心包+aop+事务相关
4.2 xml配置方式
(1)配置JDBC事务类,交给spring管理
org.springframework.jdbc.datasource.DataSourceTransactionManager
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/>
(2)配置切面
<!--配置通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!--配置通知方法,具体被增强的方法--> <tx:attributes> <tx:method name="save" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!--配置切面--> <aop:config> <!--切入点, 可能被增强的方法--> <aop:pointcut id="myPointCut" expression="execution(* cn.getword.service.impl.*.*(..))" /> <!--配置增强和切入点的关系--> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut" /> </aop:config>
4.3 xml和注解组合配置
<!-- JDBC事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 启用支持annotation注解方式事务管理 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
@Service @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false) public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; }
end