SSM框架搭建

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_34629467/article/details/79451281

spring+springmvc+mybatis的框架搭建方式,这里列出

①jar包搭建

②maven项目

首先列出方式一的搭建方式:

资源准备:

jar包
spring-aop-4.0.6.RELEASE.jar 面向切面编程
spring-aspects-4.0.6.RELEASE.jar 切面
spring-beans-4.0.6.RELEASE.jar JavaBean模块
spring-context-4.0.6.RELEASE.jar 上下文模块
spring-core-4.0.6.RELEASE.jar 核心模块
spring-expression-4.0.6.RELEASE.jar spring的表达式
spring-jdbc-4.0.6.RELEASE.jar spring对JDBC处理进行了封装
spring-orm-4.0.6.RELEASE.jar 对象关系映射
spring-tx-4.0.6.RELEASE.jar 事务处理
spring-web-4.0.6.RELEASE.jar spring对web开发的支持
spring + springmvc 整合
spring-webmvc-4.0.6.RELEASE.jar springmvc和spring的支持
数据库驱动包
mysql.jar
mybatis数据库处理半自动对象关系映射包
mybatis-3.2.5.jar
日志处理包

log4j-1.2.16.jar

其他包
javassist-3.11.0.GA.jar
commons-logging-1.1.3.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar 切面织入源码包
com.springsource.org.aopalliance-1.0.0.jar aop源码包
com.springsource.net.sf.cglib-2.2.0.jar cglib动态代理包
c3p0-0.9.1.jar 开源的JDBC连接池
asm-tree-3.3.jar ASM是小巧轻便的 Java 字节码操控框架 ,它能方便地生成和改造 Java 代码
提供了基于对象的表现形式
asm-commons-3.3.jar 
提供了基于事件的表现形式
asm-3.3.jar 
整合插件包

mybatis-spring-1.2.0.jar mybatis和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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    
        
<!-- 自动扫描 -->

<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_demo"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>

<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mappers.xml文件 -->
<property name="mapperLocations" value="classpath:com/java1234/mappers/*.xml"></property>
<!-- mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.java1234.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>


<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 配置事务通知属性 -->  
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <!-- 定义事务传播属性 -->  
        <tx:attributes>  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>  
    </tx:advice>  
  
    <!-- 配置事务切面 -->  
    <aop:config>  
        <aop:pointcut id="serviceOperation"  
            expression="execution(* com.java1234.service.*.*(..))" />  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
    </aop:config>  

</beans>

整合的mybatis文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 别名 -->
<typeAliases>
<package name="com.java1234.entity"/>
</typeAliases>

</configuration>


日志文件:

log4j.rootLogger=info,appender1,appender2
log4j.appender.appender1=org.apache.log4j.ConsoleAppender 
log4j.appender.appender2=org.apache.log4j.FileAppender 
log4j.appender.appender2.File=G:/log/logFile.txt
log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout

log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout  

整合的spring-mvc.xml

<?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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">    

<!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.java1234.controller" />
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"></property>
</bean>

</beans>  


插入src代码目录:



贴上具体代码:

首先是实体类:

package com.java1234.entity;

public class User {
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

然后是操作数据库的dao层

package com.java1234.dao;
import com.java1234.entity.User;
public interface UserDao {
public User login(User user);

}

以及对应的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.java1234.dao.UserDao">
<resultMap type="User" id="UserResult">
<result property="id" column="id"/>
<result property="userName" column="userName"/>
<result property="password" column="password"/>
</resultMap>
<select id="login" parameterType="User" resultMap="UserResult">
select * from t_user where userName=#{userName} and password=#{password}
</select>

</mapper> 

接下来就是:服务层与控制器层

package com.java1234.service;


import com.java1234.entity.User;


public interface UserService {


public User login(User user);
}

package com.java1234.service.impl;


import javax.annotation.Resource;


import org.springframework.stereotype.Service;


import com.java1234.dao.UserDao;
import com.java1234.entity.User;
import com.java1234.service.UserService;


@Service("userService")
public class UserServiceImpl implements UserService{


@Resource
private UserDao userDao;

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


}


最后是控制器:

package com.java1234.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import com.java1234.entity.User;
import com.java1234.service.UserService;


@Controller
@RequestMapping("/user")
public class UserController {


@Resource
private UserService userService;

@RequestMapping("/login")
public String login(User user,HttpServletRequest request){
User resultUser=userService.login(user);
if(resultUser==null){
request.setAttribute("user", user);
request.setAttribute("errorMsg", "用户名或密码错误!");
return "index";
}else{
HttpSession session=request.getSession();
session.setAttribute("currentUser", resultUser);
return "redirect:/success.jsp";
}
}

}


当然还有启动tomcat服务器时加载的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSM_T</display-name>
 <display-name>MyBatisPro05</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  
    <!-- Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 添加对springmvc的支持 -->
<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-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

第一次表述的项目搭建demo有些乱,但就目前而言还只是个人的笔记而已!!!

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/login.do" method="post">
userName:<input type="text" name="userName" value="${user.userName }"/><br/>
password:<input type="password" name="password" value="${user.password }"><br/>
<input type="submit" value="login"/><font color="red">${errorMsg }</font>
</form>
</body>

</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎:${currentUser.userName }
</body>

</html>


最后贴上一个数据库demo的表结构:



DROP TABLE IF EXISTS `users`;


CREATE TABLE users (
  id int(11) NOT NULL AUTO_INCREMENT,
  username varchar(20) COLLATE utf8_bin DEFAULT NULL,
  password varchar(20) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


以上就是完整的搭建,具体过程就先不进行解释了,因为我自己也还有一些疑问,目前先记录下来:

①我想明白的地方是: tomcat启动的时候加载了web.xml,然后在web.xml加载了根路径下的spring的相关配置文件,

然后是spring配置文件中的bean进行注入吗?

②整体来说: spring的aop还是没弄的彻底,明天继续spring_aop的相关学习!

未完.......












展开阅读全文

没有更多推荐了,返回首页