Spring+Spring+Hibernate环境搭建

2020-12-13 15:19

阅读:316

标签:信息   plugins   rda   findall   public   dispatch   database   统计   mes   

 

一、引入lib包

pom.xml,引入需要的jar包

xml version="1.0" encoding="UTF-8"?>

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.0modelVersion>

  groupId>com.kszsagroupId>
  artifactId>sshtartifactId>
  version>1.0-SNAPSHOTversion>
  packaging>warpackaging>

  name>ssht Maven Webappname>
  
  url>http://www.example.comurl>

  properties>
    
    spring.version>4.0.9.RELEASEspring.version>
    
    hibernate.version>4.1.0.Finalhibernate.version>
    hibernate-validator.version>4.2.0.Finalhibernate-validator.version>
    
    druid.version>1.1.9druid.version>
    
    mysql.version>5.1.27mysql.version>
    
    junit.version>4.7junit.version>

    
    
    jackson.version>1.8.4jackson.version>
    json-lib.version>2.1json-lib.version>
    fastjson-lib.version>1.2.31fastjson-lib.version>
    gson.version>2.2.4gson.version>

    
    tomcat.version>2.2tomcat.version>
    jetty.version>7.6.14.v20131031jetty.version>
    webserver.port>8080webserver.port>

    project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    maven.compiler.source>1.7maven.compiler.source>
    maven.compiler.target>1.7maven.compiler.target>
  properties>

  dependencies>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-aspectsartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-aopartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-context-supportartifactId>
      version>${spring.version}version>
    dependency>
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-testartifactId>
      version>${spring.version}version>
    dependency>
    
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-ormartifactId>
      version>${spring.version}version>
    dependency>
    
    dependency>
      groupId>org.springframeworkgroupId>
      artifactId>spring-webmvcartifactId>
      version>${spring.version}version>
    dependency>
    
    
    dependency>
      groupId>javax.servletgroupId>
      artifactId>javax.servlet-apiartifactId>
      version>3.0.1version>
      scope>providedscope>
    dependency>
    dependency>
      groupId>javax.servletgroupId>
      artifactId>jsp-apiartifactId>
      version>2.0version>
      scope>providedscope>
      exclusions>
        exclusion>
          groupId>javax.servletgroupId>
          artifactId>servlet-apiartifactId>
        exclusion>
      exclusions>
    dependency>
    dependency>
      groupId>javax.servletgroupId>
      artifactId>jstlartifactId>
      version>1.2version>
    dependency>

    
    dependency>
      groupId>org.aspectjgroupId>
      artifactId>aspectjrtartifactId>
      version>1.8.12version>
    dependency>
    dependency>
      groupId>org.aspectjgroupId>
      artifactId>aspectjweaverartifactId>
      version>1.8.12version>
    dependency>

    

    
    dependency>
      groupId>org.hibernategroupId>
      artifactId>hibernate-coreartifactId>
      version>${hibernate.version}version>
    dependency>
    dependency>
      groupId>org.hibernategroupId>
      artifactId>hibernate-ehcacheartifactId>
      version>${hibernate.version}version>
    dependency>

    
    dependency>
      groupId>com.alibabagroupId>
      artifactId>druidartifactId>
      version>${druid.version}version>
      
    dependency>

    
    dependency>
      groupId>mysqlgroupId>
      artifactId>mysql-connector-javaartifactId>
      version>${mysql.version}version>
      scope>runtimescope>
    dependency>
    
    
    dependency>
      groupId>junitgroupId>
      artifactId>junitartifactId>
      version>${junit.version}version>
    dependency>

    
    
    dependency>
      groupId>org.codehaus.jacksongroupId>
      artifactId>jackson-mapper-aslartifactId>
      version>${jackson.version}version>
    dependency>
    dependency>
      groupId>net.sf.json-libgroupId>
      artifactId>json-libartifactId>
      version>${json-lib.version}version>
      classifier>jdk15classifier>
    dependency>
    dependency>
      groupId>com.alibabagroupId>
      artifactId>fastjsonartifactId>
      version>${fastjson-lib.version}version>
    dependency>
    dependency>
      groupId>com.google.code.gsongroupId>
      artifactId>gsonartifactId>
      version>${gson.version}version>
    dependency>
    

  dependencies>

  build>
    finalName>sshtfinalName>
    pluginManagement>
      plugins>
        plugin>
          artifactId>maven-compiler-pluginartifactId>
          version>2.3.2version>
          configuration>
            source>1.7source>
            target>1.7target>
            encoding>UTF-8encoding>
          configuration>
        plugin>
        
        plugin>
          groupId>org.apache.tomcat.mavengroupId>
          artifactId>tomcat7-maven-pluginartifactId>
          version>${tomcat.version}version>
          configuration>
            port>${webserver.port}port>
            path>/${project.artifactId}path>
            uriEncoding>${project.build.sourceEncoding}uriEncoding>
          configuration>
        plugin>

        
        plugin>
          groupId>org.apache.maven.pluginsgroupId>
          artifactId>maven-surefire-pluginartifactId>
          version>2.18.1version>
          configuration>
            skipTests>trueskipTests>
          configuration>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

二、编写配置文件

编写Spring的配置文件applicationContext.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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-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/aop
         http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
         http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">

    
    context:component-scan base-package="com.kszsa">
        
        context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
beans>

springMVC的配置文件 spring-mvc.xml

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:p="http://www.springframework.org/schema/p" 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-3.0.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://cxf.apache.org/jaxws
           http://cxf.apache.org/schemas/jaxws.xsd">

    
    mvc:annotation-driven/>

    
    context:component-scan base-package="com.kszsa" use-default-filters="false">
        context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>

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

    
    mvc:default-servlet-handler/>

beans>

数据库连接信息配置文件config.properties

#database connection config
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/xcjgk?useUnicode=true&characterEncoding=utf-8
jdbc.username = root
jdbc.password = root
diver_name=com.mysql.jdbc.Driver

validationQuery.sql=SELECT 1

#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto =update

hibernate配置文件 spring-hibernate.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
       default-autowire="byName" default-lazy-init="false">

    
    
    context:property-placeholder location="classpath:config.properties"/>

    
    bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        property name="url" value="${jdbc.url}" />
        property name="username" value="${jdbc.username}" />
        property name="password" value="${jdbc.password}" />
        
        property name="initialSize" value="0" />
        
        property name="maxActive" value="50" />
        
        property name="minIdle" value="5" />

        
        property name="maxWait" value="60000" />

        
        property name="timeBetweenEvictionRunsMillis" value="60000" />
        
        property name="minEvictableIdleTimeMillis" value="300000" />

        property name="validationQuery" value="${validationQuery.sql}" />
        property name="testOnBorrow" value="false" />
        property name="testOnReturn" value="false" />
        property name="testWhileIdle" value="true" />

        
        property name="filters" value="stat" />

        
        property name="removeAbandoned" value="true" />
        
        property name="removeAbandonedTimeout" value="3600" />
        
        property name="logAbandoned" value="true" />
        
        property name="connectProperties">
            props>
                prop key="remarksReporting">trueprop>
            props>
        property>

    bean>

    
    bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        property name="dataSource" ref="dataSource">property>
        property name="packagesToScan" value="com.kszsa">property>
        property name="hibernateProperties">
            props>
                prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}prop>
                prop key="hibernate.dialect">${hibernate.dialect}prop>
                prop key="hibernate.show_sql">${hibernate.show_sql}prop>
                prop key="hibernate.format_sql">${hibernate.format_sql}prop>
            props>
        property>
    bean>

    
    bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        property name="sessionFactory" ref="sessionFactory">property>
    bean>

    
    tx:annotation-driven transaction-manager="transactionManager"/>


beans>

编写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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    display-name>sshtdisplay-name>

    
    filter>
        filter-name>encodingFilterfilter-name>
        filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        init-param>
            param-name>encodingparam-name>
            param-value>UTF-8param-value>
        init-param>
        init-param>
            param-name>forceEncodingparam-name>
            param-value>trueparam-value>
        init-param>
    filter>
    filter-mapping>
        filter-name>encodingFilterfilter-name>
        url-pattern>/*url-pattern>
    filter-mapping>

    
    context-param>
        param-name>contextConfigLocationparam-name>
        param-value>classpath:applicationContext.xml,classpath*:spring-*.xmlparam-value>

    context-param>

    listener>
        listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    

    
    servlet>
        servlet-name>spring-mvcservlet-name>
        servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        init-param>
            param-name>contextConfigLocationparam-name>
            param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
        load-on-startup>1load-on-startup>
    servlet>
    servlet-mapping>
        servlet-name>spring-mvcservlet-name>
        url-pattern>/url-pattern>
    servlet-mapping>
    


web-app>

至此配置文件编写完成

三、编写bean

编写User.java

package com.kszsa.user;

import javax.persistence.*;

@Entity
@Table(name="user")
public class User {
    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name="name")
    private String name;

    @Column(name="description")
    private String description;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

四、编写 DAO

编写通用接口ICommonDao.java

package com.kszsa.common.dao;

import java.io.Serializable;
import java.util.List;

public interface ICommonDaoextends Serializable> {

    T load(PK id);

    T get(PK id);

    List findAll();

    void persist(T entity);

    void add(T entity);

    void update(T entity);

    void delete(PK id);

    void flush();


}

编写UserDao.java

package com.kszsa.user.dao;

import com.kszsa.common.dao.ICommonDao;
import com.kszsa.user.User;


public interface UserDao extends ICommonDao {

}

编写UserDao


评论


亲,登录后才可以留言!