SSM框架柱整合思路
- WEB项目一般的流程都是基于MVC的,包也就是Dao、Server、Servlet以及我们的页面,那么使用SSM框架整合也就是分类进行整合
- SpringMVC(Controller):管理请求分发的bean
- Service:Spring管理Service所有的实现类的bean
- Dao(Mybatis):Spring会生成Mybatis接口的代理的实现类
- Spring:整合各整,以管理项目
项目结构
- 下面我们按照Dao、Service、controller的思路进行整合
Dao的整合
- 需要讲Mybatis中代理实现类变成bean
- Spring管理SqlSessionFactory以及dataSource对象
Spring配置:
<beans xmlns="http://www.springframework.org/schema/beans"
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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="password" value="${password}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="5"></property>
</bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis/MybatisMainCof.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao"></property>
<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
</bean>
</beans>
- 这里的相关配置前面博客都有介绍
- 分别是导入db数据源的配置
- 配置dataSource
- 配置SqlSessionFactory
- 基于扫描包的形式配置bean
MybatisMainCof.xml:
无内容
UserDao.java以及UserDao.xml
<mapper namespace="dao.UserDao">
<select id="findAll" resultType="domain.User">
select * from tb_user
</select>
<select id="findById" parameterType="int" resultType="domain.User">
select * from tb_user where id=#{id}
</select>
<update id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</update>
<insert id="add" parameterType="domain.User">
insert into tb_user(id,username,password) values(#{id},#{username},#{password})
</insert>
<update id="edit" parameterType="domain.User">
update tb_user
<set>
<if test="id!=null">password=#{password},</if>
<if test="id!=null">username=#{username},</if>
</set>
where id=#{id}
</update>
</mapper>
public interface UserDao {
public List<User> findAll();
public List<User> findById(int id);
public void deleteById(int id);
public void add(User user);
public void edit(User user);
}
- 以上就完成了Dao的整合了,其实代码很简单,比以前用Mybatis节省了很多代码量了
- 这里注意接口类和mapper类是同名同包哦
Service整合
- 将Service的实现类变为bean
- 建立Service以及Dao之间的关系
Service的Spring配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="server"></context:component-scan>
</beans>
- 主要配置就是开启注解以及扫描包
接口以及实现类:
public interface UserServer {
public List<User> findAll();
public void deleteById(int id);
public List<User> findById(int id);
public void add(User user);
public void edit(User user);
}
@Service
public class UserServerImpl implements UserServer{
@Resource
UserDao UserDao;
public UserDao getUserDao() {
return UserDao;
}
public void setUserDao(UserDao userDao) {
UserDao = userDao;
}
@Override
public List<User> findAll() {
// TODO 自动生成的方法存根
return UserDao.findAll();
}
@Override
public void deleteById(int id) {
// TODO 自动生成的方法存根
UserDao.deleteById(id);
}
@Override
public List<User> findById(int id) {
// TODO 自动生成的方法存根
return UserDao.findById(id);
}
@Override
public void add(User user) {
// TODO 自动生成的方法存根
UserDao.add(user);
}
@Override
public void edit(User user) {
// TODO 自动生成的方法存根
UserDao.edit(user);
}
}
- 这里我们需要注意,因为我们是配置的bean,同时是基于注解
- 所以我们需要给UserDao一个get、set方法
- 同时我们需要配置注解,如果方便也可以使用自动装配
- 其余的就是直接调用UserDao的方法了
- 整体而言还是比较简单
Controller整合
-
配置servlet,以及参数对应的SpringMVC
-
controller的实例
-
视图解析
web.xml
<servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/SpringMVC.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
SpringMVC
<beans xmlns="http://www.springframework.org/schema/beans"
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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<mvc:annotation-driven ></mvc:annotation-driven>
<context:component-scan base-package="controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 这里的配置一样和前面博客介绍的一样
- 分别是开启注解
- 配置扫描包
- 解析视图路径
UserController
@Controller
public class UserController {
@Resource
UserServer userServer;
public UserServer getUserServer() {
return userServer;
}
public void setUserServer(UserServer userServer) {
this.userServer = userServer;
}
@RequestMapping("/findAll.action")
public String findAll(HttpServletRequest req) {
List<User> list=userServer.findAll();
for (User user : list) {
System.out.println(user);
}
req.setAttribute("list", list);
return "result";
}
}
- 这里的配置和前面的service的配置差不多,都是配置一个bean
- 然后给出get、set方法
- 这里只不过多了一个
@RequestMapping("/findAll.action")
请求页面
</head>
<body>
<a href="findAll.action">点击帅帅哒的我</a>
</body>
</html>
响应页面
<body>
<table border="1">
<tr>
<form action="findById.action">
<td colspan="3"><input type="text" name="id"/></td>
<td><input type="submit" value="搜索"/></td>
</form>
</tr>
<tr>
<td colspan="2"><a href="findAll.action">查找全部</a></td>
<td colspan="2"><a href="add.jsp">新增用户</a></td>
</tr>
<tr>
<td>ID</td>
<td>用户名</td>
<td>密码</td>
<td>操作</td>
</tr>
<c:forEach items="${list }" var="user">
<tr>
<td>${user.id }</td>
<td>${user.username }</td>
<td>${user.password }</td>
<td>
<a href="delete.action?id=${user.id}">删除</a>
<a href="editPre.action?id=${user.id}">编辑</a>
</td>
</tr>
</c:forEach>
</table>
</body>
数据库设计
代码运行结果: