Spring+SpringMVC+Hibernate框架完整版配置(基于xml)

来源:互联网 发布:美萍软件怎么样 编辑:程序博客网 时间:2024/06/05 20:49

1.整体结构:

2.Maven之pom依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.gdq</groupId><artifactId>ssh</artifactId><version>1.0</version><packaging>war</packaging>
<dependencies><!--Spring ORM --><!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>4.3.8.RELEASE</version></dependency><!--Spring WebMVC --><!-- https://mvnrepository.com/artifact/org.springframework/spring-web --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.8.RELEASE</version></dependency><!--Spring 切面 --><!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.3.8.RELEASE</version></dependency><!-- Hibernate --><!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>5.2.10.Final</version></dependency><!--数据库连接池 --><!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><!--数据库连接驱动 --><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.39</version></dependency><!--JSTL --><!-- https://mvnrepository.com/artifact/javax.servlet/jstl --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- https://mvnrepository.com/artifact/taglibs/standard --><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><!--Servlet --><!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency></dependencies><build><plugins><!-- java编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build></project>

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"version="3.1"><display-name>ssh</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><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></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><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:springMVC.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>message.jsp</welcome-file></welcome-file-list></web-app>
4.整合配置:(applicationContext.xml)

<?xml version="1.0" encoding="UTF-8"?><!--约束文件,基于schema写的,xsd 需要什么就配置文件 --><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:tx="http://www.springframework.org/schema/tx"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.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd          http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop.xsd"><!--1、引用外部的配置文件 --><context:property-placeholder location="classpath:dbconfig.properties" /><!--2、配置数据库连接池 --><bean id="ds"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!--3、配置SessionFactory --><bean id="sessionFactory"class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><!--1、配置数据库连接池 --><property name="dataSource" ref="ds"></property><!--2、配置Hibernate的信息 --><property name="hibernateProperties"><props><!--方言 --><prop key="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</prop><!--显示建表语句 --><prop key="hibernate.show_sql">true</prop><!--格式化SQL语句 --><prop key="hibernate.format_sql">true</prop><!--自动创建表 --><prop key="hibernate.hbm2ddl.auto">update</prop><!--使用getCurrentSession --><!-- <prop key="hibernate.current_session_context_class">thread</prop> --><!-- <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop> --></props></property><!--3、配置映射文件mappingLocations:配置映射文件路径,支持*mappingJarLocationss:加载jar中映射mappingDirectoryLocations:加载指定目录的映射文件mappingResources:映射文件完整路径  --><property name="mappingLocations" value="classpath:com/gdq/domain/*.hbm.xml"></property></bean><!--4、配置事务管理器 --><bean id="txManager"class="org.springframework.orm.hibernate5.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!--5、配置事务通知  --><tx:advice id="txAdvice" transaction-manager="txManager"></tx:advice><!--6、声明式事务的AOP --><aop:config proxy-target-class="true"><aop:pointcut expression="execution(* com.gdq.dao.impl.*.*(..))" id="pt"/><!--引入通知  --><aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/></aop:config><!--7、配置dao  --><bean id="dao" class="com.gdq.dao.impl.MsgDaoImpl"><property name="sessionFactory" ref="sessionFactory"></property></bean><!--8、配置service  --><bean id="service" class="com.gdq.service.impl.MsgServiceImpl"><property name="dao" ref="dao"></property></bean></beans>
5.SpringMVC

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd"><!--注解式实现控制器 --><context:component-scan base-package="com.gdq.web.controller"></context:component-scan><!--配置所有页面的前缀和后缀 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--配置页面的前缀 --><property name="prefix" value="/WEB-INF/jsp/"></property><!--配置后缀 --><property name="suffix" value=".jsp"></property></bean></beans>
6.数据库配置(dbconfig.properties)

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/gdq?characterEncoding=UTF-8jdbc.username=rootjdbc.password=123456

7.domain层(实体层)

package com.gdq.domain;public class Msg {private long id;private String name;private String message;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public Msg(String name, String message) {super();this.name = name;this.message = message;}public Msg() {super();}}
配置.xml

<?xml version="1.0" encoding="UTF-8"?><!--约束文件 --><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.gdq.domain"><class name="Msg" table="tb_msg"><id name="id"><generator class="native"></generator></id><property name="name" length="20" /><property name="message" length="200" /></class></hibernate-mapping>



8.dao层即service实现:








原创粉丝点击