开发者学堂课程【Java Web 开发系列课程:Spring 框架入门:spring 整合 mybatis 03】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/538/detail/7355
ssm 整合
内容介绍:
一、 导入 jar 包
二、 web.xml 配置文件
三、 mybatis 配置文件 (mybatis.cfg.xml)
四、 编码
一、 导入 jar 包
aopalliance.jar
asm-3.3.1.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
aspectiweaver.jar
cglib-2.2.2.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.19.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.20-bin.jar
ognl-3.0.5.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
struts2-core-2.3.4.jar
struts2-spring-plugin-2.3.4.jar
xwork-core-2.3.4.jar
二、 web.xml 配置文件
contextConfigLocation
classpath:beans.xml
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
三、 mybatis 配置文件 (mybatis.cfg.xml)
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
四、 编码
1. UserDao 接口
public interface UserDao {
public List getAll();
}
2. UserDaoImpl 实现
public class UserDapoImpl extends SqlSessionDaoSupport
implements UsexDao{
public List getAll() {
return
this.getSqlSession().selectList("cn.sxt.vo.user.mapper.getAll");
}
}
3. UserServise 接口
public interface UserServise {
public List getAll();
}
4. UserServiceImpl 实现
public class UserServiceImpl implements UserService{
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
public List getAll() {
//TODO Auto-generated method stub
return userDao.getAll();
}
}
5. UserAction
public class UserAction {
private List list;
private UserService userService;
public String list(){
list = userService.getAll();
return "success";
}
public List getList(){
return list;
}
pubiic void setList(List list) {
this.list = list;
}
public UserService.getUserService(){
return userService;
}
public void setUserService(UserSenxice.usenSenvice) {
this.userService = userService;
}
}
6. Spring 配置文件
1)
applicationContext.xml
xmlns:xsi="http://www.w3.org/2001/XMLschema-instance"
xmlns:aop=http://www.springframework.org/schema/aop
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
value="com.mysql.jdbc.Driver"/>
value="jdbc:mysql://Localhost:3306/test"/>
class="org.mybatis.spring.SaLSessionFactoryBean">
value="classpath:cn/sxt/vo/*.mapper.xmL">
2)
User.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframetwork.org/schema/ aop"
xmlns:tx="http://www.springframework.org/ schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
ref="sqlSessionFactory"/>
class="cn.sxt.service.impl.UserSeviceImpl">
3)
Struts.xml
4) User.xml
extends= "struts-default">
list.jsp