1、整体图:
2、导入依赖:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!--servlet的版本号-->
<servlet-version>4.0.0</servlet-version>
<!--mysql的版本号-->
<mysql-version>5.1.46</mysql-version>
<!--c3p0的版本号-->
<c3p0-version>0.9.5.2</c3p0-version>
<!--hibernate的版本号-->
<hibernate-version>5.2.13.Final</hibernate-version>
<!--struts2的版本号-->
<struts2-version>2.5.16</struts2-version>
<!--spring的版本号-->
<spring-version>4.3.13.RELEASE</spring-version>
<!--fastjson的版本号-->
<fastjson-version>1.2.47</fastjson-version>
</properties>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--配置servlet的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-version}</version>
</dependency>
<!--配置mysql的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-version}</version>
</dependency>
<!--配置c3p0的依赖-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0-version}</version>
</dependency>
<!--配置hibernate的依赖-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-version}</version>
</dependency>
<!--配置struts2的依赖-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2-version}</version>
</dependency>
<!--配置struts2-spring的桥接包-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2-version}</version>
</dependency>
<!-- 配置struts2的注解式依赖(注解式与配置式的不同) -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts2-version}</version>
</dependency>
<!--配置spring的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- JSON格式的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson-version}</version>
</dependency>
在build里面加上:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
3、创建pojo包的实体类User:
/**
* 用户表的实体类
*/
@Entity//指定为实体类
@Table(name = "user")//对象数据库的表
public class User implements Serializable {
@Id
@GenericGenerator(name = "myid", strategy = "guid")
@GeneratedValue(generator = "myid")//指名主键
private String user_id;//用户编号(这里的属性名称必须和数据库表的列名一致,不然无法映射)
@Column(name = "user_name")
private String user_name;//用户姓名
@Column(name = "user_pwd")
private String user_pwd;//用户密码
@Column(name = "user_type")
private String user_type;//用户类型
public User() {
}
public User(String user_id, String user_name, String user_pwd, String user_type) {
this.user_id = user_id;
this.user_name = user_name;
this.user_pwd = user_pwd;
this.user_type = user_type;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_pwd() {
return user_pwd;
}
public void setUser_pwd(String user_pwd) {
this.user_pwd = user_pwd;
}
public String getUser_type() {
return user_type;
}
public void setUser_type(String user_type) {
this.user_type = user_type;
}
@Override
public String toString() {
return "User{" +
"user_id='" + user_id + '\'' +
", user_name='" + user_name + '\'' +
", user_pwd='" + user_pwd + '\'' +
", user_type='" + user_type + '\'' +
'}';
}
}
4、mapper包:
4.1、获取sessionFactory的类:
/**
* 基础类
*/
public class BaseDao {
/**
* 首先在静态代码块中生成出sessionFactory
*/
@Resource(name = "sessionFactoryBean")//获取application-public.xml的sessionFactory的工厂,name值必须和bean的id值一致
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
sessionFactory = sessionFactory;
}
/**
* @return Session
* @Title: getSession
* @Description: 获取session
*/
public Session getSession() {
return sessionFactory.getCurrentSession();
}
}
4.2、mapper包的接口:
/**
* 用户表的mapper包的接口
*/
public interface IUserMapper {
/**
* 用户表登录的方法
*
* @param user
* @return
*/
public User UserLogin(User user);
}
4.3、mapper包的实现类:
/**
* 用户表的mapper包的实现类
*/
@Repository("IUserMapper")//创建一个IUserMapper的bean
public class UserMapperImpl extends BaseDao implements IUserMapper {
@Override
public User UserLogin(User user) {
return (User) getSession().createQuery("from User WHERE user_name=:user_name AND user_pwd=:user_pwd")
.setParameter("user_name", user.getUser_name()).setParameter("user_pwd", user.getUser_pwd()).getSingleResult();
}
}
5、service包:
5.1、service包的接口:
扫描二维码关注公众号,回复:
4188326 查看本文章
/**
* 用户表的service包的接口
*/
public interface IUserService {
/**
* 用户表登录的方法
*
* @param user
* @return
*/
public User UserLogin(User user);
}
5.2、service包的实现类:
/**
* 用户表的service包的实现类
*/
@Repository("IUserService")//创建一个IUserService的bean
public class UserServiceImpl implements IUserService {
//得到IUserMapper的bean
@Resource(name = "IUserMapper")
private IUserMapper iUserMapper;
public IUserMapper getiUserMapper() {
return iUserMapper;
}
public void setiUserMapper(IUserMapper iUserMapper) {
this.iUserMapper = iUserMapper;
}
@Override
public User UserLogin(User user) {
return iUserMapper.UserLogin(user);
}
}
6、action包:
6.1、action包的数据交互类:
/**
* 国家表的action包的数据交互类
*/
@Scope(value = "prototype")//scope默认是单例模式,prototype:原型模式,每次获取Bean的时候会有一个新的实例
@ParentPackage("struts-default")//父包注解
@Results({@Result(name = "success", location = "/index.jsp"),
@Result(name = "error", location = "/404.jsp")})//name为方法返回值,location为跳转页面
public class NationAction extends ActionSupport implements ModelDriven<User> {
// struts2的数据封装:
// 表达式封装和模型封装的相同点和不同点:
// 相同点:都可以把数据封装到实体类中。
// 不同点:模型封装是因为类继承了ModelDriven的接口,一个类只能继承一个接口,所以只能把数据封装到一个实体类中,
// 而表达式封装可以把数据封装到多个实体类中。
/**
* 实例化Nation对象
*
* @return
*/
private User user = new User();
@Override
public User getModel() {
return user;
}
//得到IUserService的bean
@Resource(name = "IUserService")
private IUserService iUserService;
public IUserService getiUserService() {
return iUserService;
}
public void setiUserService(IUserService iUserService) {
this.iUserService = iUserService;
}
/**
* 据国家表信息查询国家表与省份表的所有信息
*
* @return
*/
@Action("UserLogin")
public String UserLogin() throws Exception {
//得到登录的方法:如果得到登录对象,则成功,否则失败
if (iUserService.UserLogin(user) != null) {
return SUCCESS;
}
return ERROR;
}
}
7、配置spring核心文件信息(application-public.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- 开启自动扫描 -->
<context:component-scan
base-package="com.zking"/>
<!-- 注解事务 -->
<tx:annotation-driven
transaction-manager="transactionManager"/>
<!-- 配置数据库连接池 -->
<bean id="datasource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--用户名 -->
<property name="user" value="root"></property>
<!-- 密码 -->
<property name="password" value="sasa"></property>
<!-- 驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<!-- 数据库连接地址 -->
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/chencao?characterEncoding=utf-8"></property>
<!-- 初始化连接数 -->
<property name="initialPoolSize" value="3"></property>
<!-- 最大连接数 -->
<property name="maxPoolSize" value="100"></property>
</bean>
<!-- 配置sessionfactory -->
<bean id="sessionFactoryBean"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 引入数据源 -->
<property name="dataSource" ref="datasource"></property>
<!-- 加载hibernate配置文件 -->
<!-- 引入hibernate配置文件 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate5.SpringSessionContext
</prop>
</props>
</property>
<!-- 映射实体文件位置 -->
<property name="packagesToScan">
<array>
<value>com.zking.pojo</value>
</array>
</property>
</bean>
<!-- 配置事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryBean"></property>
</bean>
<!-- 配置事务属性 -->
<tx:advice id="myAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="edit*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务属性切点 -->
<aop:config>
<aop:pointcut
expression="execution(* com.zking.service.*.*(..))"
id="myCut"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="myCut"/>
</aop:config>
</beans>
8、在web.xml文件中配置spring和struts2的拦截器:
<!-- spring的拦截器 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-public.xml</param-value>
</context-param>
<!-- 在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?
下面我们来看看Spring框架给我们提供过滤器CharacterEncodingFilter
这个过滤器就是针对于每次浏览器请求进行过滤的,然后再其之上添加了父类没有的功能即处理字符编码。
其中encoding用来设置编码格式,forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。-->
<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>
<!-- 加载struts2:配置struts2的过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
9、页面访问(使用s标签库则必须导入标签库:<%@taglib prefix="s" uri="/struts-tags" %>):
<--action的属性值为action包的访问地址-->
<s:form action="UserLogin" method="post">
<!--这里的标签name属性值必须和实体类的属性一致,不然无法获取值-->
<s:textfield name="user_name" label="用户名"></s:textfield>
<s:password name="user_pwd" label="密码"></s:password>
<s:submit value="登录"></s:submit>
</s:form>
10、SSH——注解式的总结:
10.1、hibernate的注解体现在:pojo的包。比配置式少了XXX.hbm.xml,直接在pojo包的实体类配置了注解信息。
10.2、spring的注解体现在:mapper包、service包。不需要再spring得xml中配置bean了,直接在类上面实现动态注入。
10.3、struts2得注解体现在:action包。比配置式少了struts.xml的struts2的核心配置文件,直接在action包的数据交互类上配置注解信息。