ssm框架整合

Posted yaxiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm框架整合相关的知识,希望对你有一定的参考价值。

#弄了了我好久天的框架整合,今天终于弄好了。做一个小dome#

1、构建web工程

技术分享图片

 

2、把需要的jar包导进去

技术分享图片

 

 3、springmvc 配置

  <!--  spring 自动扫描 base-package下面的包或子包下面的Java文件,将扫描到有Spring的相关

  注解的类注册为Spring的bean -->

  <context:component-scan base-package="com.zspt.lab.web"></context:component-scan>

  <!--设置配置方案 -->
  <mvc:annotation-driven />

  <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>

 

 

4、db.properties 配置

  jdbc.driver=com.mysql.jdbc.Driver
  jdbc.url=jdbc:mysql://localhost:3306/admin_zspt
  jdbc.username= #连接数据库的用户名
  jdbc.password= #连接数据库的密码

 

 

5、applicationContext-dao.xml 配置

<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:*.properties" />

<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
  <property name="driverClassName" value="${jdbc.driver}" />
  <property name="maxActive" value="10" />
  <property name="minIdle" value="5" />
</bean>

<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 数据库连接池 -->
  <property name="dataSource" ref="dataSource" />
  <!-- 加载mybatis的全局配置文件 -->
  <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>

<!-- mapple扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.zspt.lab.dao" />
</bean>

 

 

 4、applicationContext-service.xml 配置

 

  <context:component-scan base-package="com.zspt.lab.service.impl"/> 

 

 

5、applicationContext-transaction.xml 配置 

<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!-- 数据源 -->
  <property name="dataSource" ref="dataSource" />
</bean>

 

 

6、web.xml配置

<!-- 加载spring容器 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

<!-- post乱码解决 -->
<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
  </init-param>
  <!-- <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value>
  </init-param> -->
</filter>
<filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>


<!-- springmvc的前端控制器 -->
<servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 如果不配置contextConfigLocation, 则默认加载:WEB-INF/servlet的名称+"-servlet.xml" -->
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/springmvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>


<!-- 日志记录 -->
<context-param>
  <!-- 日志配置文件路径 -->
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:log4j.properties</param-value>
</context-param>

<context-param>
  <!-- 日志页面的刷新间隔 -->
  <param-name>log4jRefreshInterval</param-name>
  <param-value>6000</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

 

 

8、包装层model

  #为了节省空间,构造方法和get/set方法就不写了

private String id;

private String username;

private String password;

 

 

9、web层

public class LoginController {

  @Autowired
  public IUserService userService;

@RequestMapping(value = "/login", method = RequestMethod.GET)
  public String toLogin(Model model) {
    model.addAttribute("user", new User());
    return "util/login";
}

  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public String Login(Model model, RedirectAttributes redirectAttributes, User user) throws Exception {
    if (userService.login(user) != null) {
      redirectAttributes.addFlashAttribute("message", "登录成功");
    } else {
      redirectAttributes.addFlashAttribute("message", "登录失败");
    }
    model.addAttribute("user", user);
    return "redirect:/transfer";
  }

  @RequestMapping(value = "/transfer")
  public String transfer(Model model, User user) throws Exception {
    model.addAttribute("user", user);
    return "util/login";
  }

}

 

10、业务层service

  IUserService.java接口:

    public User login(User user) throws Exception;

 

  IUserServiceImp.java实现接口:

    #记得加注解

    @Service
    public class IUserServiceImp implements IUserService {

      @Autowired
      private IUserDao userDao;

      @Override
      public User login(User user) throws Exception {
        return userDao.login(user);
      }

    }

 

11、mapple代理的方法实现持久层dao

  •    IUserDao.java 接口:

    public User login(@Param(value="user")User user) throws Exception;

  

  

  •    IUserDao.xml: #xml文件的名称要和java文件名称一致

  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.zspt.lab.dao.IUserDao">
  <sql id="aaa">
    <if test="user!=null">
      <if test="user.username!=null and user.password!=null">
        username=#{user.username} AND password=#{user.password}
      </if>
    </if>
  </sql>


  <select id="login" resultType="user">
    SELECT * FROM administrator
    <where>
      <include refid="aaa"></include>
    </where>
  </select>

</mapper>

 

 

12、log4j.properties

### 设置###
log4j.rootLogger = debug,stdout,D,E


### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n


### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ./logs/log.log
log4j.appender.D.Append = true
## 输出DEBUG级别以上的日志
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n


### 保存异常信息到单独文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
## 异常日志文件名
log4j.appender.D.File = ./logs/error.log
log4j.appender.D.Append = true
## 只输出ERROR级别以上的日志!!!
log4j.appender.D.Threshold = ERROR
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

 

 

13、jsp页面

<form:form action="login" method="post" modelAttribute="user">
  <table>
    <tr>
      <td>用户名</td>
      <td><form:input path="username"></form:input></td>
    </tr>
    <tr>
      <td>密码</td>
      <td><form:password path="password"></form:password></td>
    </tr>
    <tr>
      <td><input type="submit" value="登录"></td>
    </tr>
    <tr>
      <td>${requestScope.message }</td>
    </tr>
  </table>
</form:form>

 

 14、测试

  用户名:zhangsan

  密码:123465

  技术分享图片

 

  数据库:

    技术分享图片

 

  结果:

    技术分享图片

 

 

 

 



































































































































































以上是关于ssm框架整合的主要内容,如果未能解决你的问题,请参考以下文章

SSM框架整合

Spring MVC 框架学习---- SSM 框架整合

ssm框架整合

SSM框架整合 环境搭建

SSM框架快速整合的实例-学生查询

SSM框架整合