第十一讲 使用注解开发
来源:互联网 发布:学习数据库系统工程师 编辑:程序博客网 时间:2024/06/06 03:22
一、spring配置文件(applicationContext.xml):
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置数据源 -->
<beanid="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="com.mysql.jdbc.Driver"></property>
<propertyname="url"value="jdbc:mysql://localhost:3306/test"></property>
<propertyname="username"value="root"></property>
<propertyname="password"value="123456"></property>
</bean>
<!-- 声明式事务配置 -->
<!-- 配置事务管理器 -->
<beanid="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!-- 配置事务通知 -->
<tx:adviceid="txAdvice"transaction-manager="txManager">
<tx:attributes>
<!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 -->
<tx:methodname="add"propagation="REQUIRED"/>
<tx:methodname="insert"propagation="REQUIRED"/>
<tx:methodname="update"propagation="REQUIRED"/>
<tx:methodname="delete"propagation="REQUIRED"/>
<tx:methodname="remove*"propagation="REQUIRED"/>
<tx:methodname="get"read-only="true"/>
<tx:methodname="*"propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 切入点应该是service.impl下的所有类的所有方法,这里为了方便测试所以直接用了dao.impl -->
<aop:pointcutexpression="execution(* com.liujie.dao.impl.*.*(..))"
id="pointcut"/>
<aop:advisoradvice-ref="txAdvice"pointcut-ref="pointcut"/>
</aop:config>
<!-- 声明式事务配置结束 -->
<!-- 配置sqlSessionFactory -->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"></property>
<propertyname="configLocation"value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 自动扫描所配置包下的所有注解
dao:@Repository("userDao")
service:@Service("userService")
action:@Controller("userAction") @Scope("prototype")
属性的注入:@Autowired
-->
<context:component-scanbase-package="com.liujie"></context:component-scan>
</beans>
二、spring注解:
UserDaoImpl.java:
@Repository("userDao")
publicclass UserDaoImpl extendsSqlSessionDaoSupport implements UserDao {
@Autowired
@Override
publicvoid setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
publicList<User> getAll() {
returnthis.getSqlSession().selectList("com.liujie.model.UserMapper.getAll");
}
}
<beanid="userDao"class="com.liujie.dao.impl.UserDaoImpl">
<propertyname="sqlSessionFactory"ref="sqlSessionFactory"></property>
</bean>
@Repository("userDao"),相当于是创建一个id="userDao"的bean;@Autowired,可以写在要注入的属性上面,也可以写在要注入
的属性的setter方法上面,注入的值是id=属性名的bean。
UserServiceImpl.java:
@Service("userService")
publicclass UserServiceImpl implementsUserService {
@Autowired
privateUserDao userDao;
publicvoid setUserDao(UserDao userDao) {
this.userDao= userDao;
}
publicList<User> getAll() {
returnuserDao.getAll();
}
}
为什么上面UserDaoImpl.java里面,@Repository("userDao")中一定要写"userDao"呢?
因为这里要注入UserDao的属性名为userDao,如果UserDaoImpl.java里面直接写@Repository,默认创建的bean的
id=类名(userDaoImpl),所以要么在UserDaoImpl.java里面写@Repository("userDao"),要么把这里privateUserDaouserDao改
写为privateUserDaouserDaoImpl。
UserAction.java:
@Controller("userAction")
@Scope("prototype")
publicclass UserAction {
privateList<User> list;
@Autowired
privateUserService userService;
publicvoid setUserService(UserService userService) {
this.userService= userService;
}
publicList<User> getList() {
returnlist;
}
publicvoid setList(List<User> list) {
this.list= list;
}
publicString list() {
list= userService.getAll();
return"success";
}
}
三、总结:
<beanid="userDao"class="com.liujie.dao.impl.UserDaoImpl">
<propertyname="sqlSessionFactory"ref="sqlSessionFactory"></property>
</bean>
<beanid="userService"class="com.liujie.service.impl.UserServiceImpl">
<propertyname="userDao"ref="userDao"></property>
</bean>
<beanid="userAction"class="com.liujie.action.UserAction"scope="prototype">
<propertyname="userService"ref="userService"></property>
</bean>
阅读全文
0 0
- 第十一讲 使用注解开发
- 第六讲 使用注解开发
- Android高级之第十一讲Hybird开发
- 第十一讲
- 第十一讲:类和对象的使用
- struts2第十一讲笔记,在页面中使用ognl表达式
- 第十一讲 JDBC编程
- 第十一讲.异常检测
- 第十一讲:重载
- Spring第六讲学习笔记,使用注解开发。Repository,Service,Controller
- 第十一讲 免费空间申请
- 第十一讲 曲线和曲面
- 读书笔记(第十一讲)Encapsulation
- 10-11Asp第十一讲
- 韩顺平网页第十一讲
- 第十一讲学习,添加地名
- Oracle-第十一讲pl-sql
- Hadoop第十一讲-搜索推荐
- POJ 1410 Intersection 笔记
- python学习之——with as语句
- eclipse快捷健大全
- 基于检索与深度神经网络的人机对话
- 用简单的方式讲解KMP算法
- 第十一讲 使用注解开发
- vue.js使用注意事项
- android 自定义Dialog
- LDD3源码分析之按页分配内存
- JAVA时间格式化处理
- yii2.0实现文件上传
- Python2.7 pip编码错误UnicodeDecodeError: 'utf8' codec can't decode byte 0xb0 in解决方法
- 关于WebView加载URL时显示一片空白的问题
- C# Parallel 并行计算测试