SSM整合
Posted kklb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM整合相关的知识,希望对你有一定的参考价值。
一、引入所有的jar文件
1. mysql数据库驱动jar
mysql-connector-java-5.1.46.jar
2. mybatis 的jar包
mybatis-3.2.2.jar
3. spring的核心jar。
commons-logging-1.1.3.jar
spring-beans-4.2.2.RELEASE.jar
spring-context-4.2.2.RELEASE.jar
spring-core-4.2.2.RELEASE.jar
spring-expression-4.2.2.RELEASE.jar
4. spring的注解。
spring-aop-4.2.2.RELEASE.jar
5. 事物处理。
spring-aspects-4.2.2.RELEASE.jar
spring-jdbc-4.2.2.RELEASE.jar
spring-tx-4.2.2.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
6. 数据源的jar包
c3p0-0.9.1.1.jar
7. springmvc的jar包。
spring-web-4.2.2.RELEASE.jar
spring-webmvc-4.2.2.RELEASE.jar
8. spring与mybatis整合的jar包
mybatis-spring-1.3.0.jar
二、配置文件
1.spring的配置文件
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 包扫描 --> <context:component-scan base-package="com.zhiyou100.klb"/> <!-- 引入属性文件 <context:property-placeholder file-encoding="utf-8" location="classpath:*.properties"/> --> <!-- 配置数据源:数据库交互的。c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="root" /> <property name="password" value="123456" /> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis" /> </bean> <!-- 定义一个SessionFactory工程类的bean ;把mybatis配置文件中的内容放在该类中--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 为该包下的实体类起别名 --> <property name="typeAliasesPackage" value="com.zhiyou100.klb.bean"/> <!-- 加载mybatis的映射文件 --> <property name="mapperLocations" value="classpath:com/zhiyou100/klb/mapper/*.xml"/> </bean> <!-- 配置mybatis接口的实现类userDao=session.getMapper(UserDao.class);相当于创建了 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- basePackage:表示哪些包下的接口要产生实现类 --> <property name="basePackage" value="com.zhiyou100.klb.dao"/> <property name="sqlSessionFactory" ref="sessionFactory"/> </bean> <!-- 定义一个事务管理类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 设置方法的属性 --> <tx:advice transaction-manager="transactionManager" id="advice"> <tx:attributes> <!-- read-only:只读。用在查询上 --> <tx:method name="query*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="select*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 切面的配置 --> <aop:config> <!-- 切点 业务层所在的包 --> <aop:pointcut expression="execution(* com.zhiyou100.klb.service.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/> </aop:config> </beans>
2.springmvc的配置文件
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 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-4.2.xsd"> <!-- 包扫描 --> <context:component-scan base-package="com.zhiyou100.klb"/> <!-- 开启注解驱动,设置编码 --> <mvc:annotation-driven/> <!-- 释放静态资源 --> <mvc:default-servlet-handler/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
3.web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SSM</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 加载springmvc的配置文件 --> <!-- 设置控制器 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> <!-- 容器在启动时加载springmvc的配置文件。访问时加载配置文件 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 加载spring的配置文件 --> <!-- 指定spring的配置文件的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <!-- 默认加载的配置文件为WEB-INF/applicationContext.xml --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
三、用户表的crud
1.com.zhiyou100.klb.bean
package com.zhiyou100.klb.bean; public class Users private int id; private String name; private int age; private String sex; public String getSex() return sex; public void setSex(String sex) this.sex = sex; public int getId() return id; public void setId(int id) this.id = id; public String getName() return name; public void setName(String name) this.name = name; public int getAge() return age; public void setAge(int age) this.age = age;
2.com.zhiyou100.klb.dao
package com.zhiyou100.klb.dao; import java.util.List; import com.zhiyou100.klb.bean.Users; public interface UsersDao /** * 查询所有 * @return */ public List<Users> getAll(); /** * 查询用户 * @return */ public Users getUser(int id); /** * 添加 * @param user */ public void add(Users user); /** * 根据ID删除 * @param id */ public void delete(int id); /** * 更新 * @param user */ public void update(Users user);
3.com.zhiyou100.klb.service
package com.zhiyou100.klb.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.zhiyou100.klb.bean.Users; import com.zhiyou100.klb.dao.UsersDao; @Service public class UserService @Autowired private UsersDao usersDao; /** * 查询所有 * @return */ public List<Users> getAll() return usersDao.getAll(); /** * 查询用户 * @return */ public Users getUser(int id) return usersDao.getUser(id); /** * 添加 * @param user */ public void add(Users user) usersDao.add(user); /** * 根据ID删除 * @param id */ public void delete(int id) usersDao.delete(id); /** * 更新 * @param user */ public void update(Users user) usersDao.update(user);
4.com.zhiyou100.klb.mapper
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zhiyou100.klb.dao.UsersDao"> <select id="getUser" parameterType="int" resultType="Users"> select * from users where id = #id </select> <select id="getAll" resultType="Users"> select * from users </select> <insert id="add" parameterType="Users" useGeneratedKeys="true" keyProperty="id"> insert into users(name,age,sex) values(#name,#age,#sex) </insert> <delete id="delete" parameterType="int"> delete from users where id = #id </delete> <update id="update" parameterType="Users"> update users set name = #name, age = #age, sex = #sex where id = #id </update> </mapper>
5.com.zhiyou100.klb.controller
package com.zhiyou100.klb.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.zhiyou100.klb.bean.Users; import com.zhiyou100.klb.service.UserService; @Controller @RequestMapping("user") public class UserController @Autowired private UserService userService; @RequestMapping("deleteUser") public String delete(int id) userService.delete(id); return "redirect:allUser"; @RequestMapping("allUser") public String all(Model model) model.addAttribute("list", userService.getAll()); return "list"; @RequestMapping("getAdd") public String getAdd() return "add"; @RequestMapping("addUser") public String add(Users user) userService.add(user); return "index"; @RequestMapping("getUpdate") public String getUpdate(int id,Model model) System.out.println(id); model.addAttribute("user", userService.getUser(id)); System.out.println(userService.getUser(id)); return "update"; @RequestMapping("updateUser") public String update(Users user) userService.update(user); return "redirect:allUser";
以上是关于SSM整合的主要内容,如果未能解决你的问题,请参考以下文章