基本环境搭建

来源:互联网 发布:淘宝客可以关闭吗 编辑:程序博客网 时间:2024/06/04 18:33

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_2_5.xsd" id="WebApp_ID" version="2.5">    <!-- 配置启动 IOC 容器的 Listener -->    <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>    <!-- 配置可以把 POST 请求转为 PUT、DELETE 请求的 Filter -->    <filter>        <filter-name>HiddenHttpMethodFilter</filter-name>        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>HiddenHttpMethodFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- 配置 OpenEntityManagerInViewFilter. 可以解决懒加载异常的问题 -->    <filter>        <filter-name>OpenEntityManagerInViewFilter</filter-name>        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>OpenEntityManagerInViewFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- 配置 SpringMVC 的 DispatcherServlet -->    <servlet>        <servlet-name>springDispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springDispatcherServlet</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

springDispatcherServlet-servlet.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:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd        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-4.0.xsd">    <!-- 配置自动扫描的包 -->    <context:component-scan base-package="com.zhou" use-default-filters="false">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>    </context:component-scan>    <!-- 配置视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"></property>        <property name="suffix" value=".jsp"></property>    </bean>    <mvc:default-servlet-handler/>    <mvc:annotation-driven></mvc:annotation-driven></beans>

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:tx="http://www.springframework.org/schema/tx"    xmlns:jpa="http://www.springframework.org/schema/data/jpa"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <!-- 配置自动扫描的包 -->    <context:component-scan base-package="com.zhou">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>    </context:component-scan>    <!-- 配置数据源 -->    <context:property-placeholder location="classpath:db.properties"/>    <bean id="dataSource"        class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="user" value="${jdbc.user}"></property>         <property name="password" value="${jdbc.password}"></property>         <property name="driverClass" value="${jdbc.driverClass}"></property>           <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>           <!-- 配置其他属性 -->    </bean>    <!-- 配置 JPA 的 EntityManagerFactory -->    <bean id="entityManagerFactory"        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <property name="jpaVendorAdapter">            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>        </property>         <property name="packagesToScan" value="com.zhou"></property>        <property name="jpaProperties">            <props>                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.format_sql">true</prop>                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>                <prop key="hibernate.cache.use_second_level_cache">true</prop>                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>                <prop key="hibernate.cache.use_query_cache">true</prop>            </props>        </property>        <property name="sharedCacheMode" value="ENABLE_SELECTIVE"></property>    </bean>    <!-- 配置事务 -->    <bean id="transactionManager"        class="org.springframework.orm.jpa.JpaTransactionManager">        <property name="entityManagerFactory" ref="entityManagerFactory"></property>        </bean>    <!-- 配置支持基于注解的事务 -->    <tx:annotation-driven transaction-manager="transactionManager"/>    <!-- 配置 SpringData -->    <jpa:repositories base-package="com.zhou"        entity-manager-factory-ref="entityManagerFactory"></jpa:repositories></beans>

db.properties:

jdbc.user=rootjdbc.password=123456jdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc\:mysql\:///sssp_crud

ehcache.xml:

http://paste.ubuntu.com/25946163/

SSSPTest.java–测试:

public class SSSPTest {    private ApplicationContext ctx = null;    {        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    }    @Test    public void testDataSource() throws SQLException {        DataSource dataSource = ctx.getBean(DataSource.class);        System.out.println(dataSource.getConnection());    }}

实体类&数据表关系:

@Cacheable@Table(name="SSSP_DEPARTMENTS")@Entitypublic class Department {    private Integer id;    private String departmentName;    @GeneratedValue    @Id    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getDepartmentName() {        return departmentName;    }    public void setDepartmentName(String departmentName) {        this.departmentName = departmentName;    }}
@Table(name="SSSP_EMPLOYEES")@Entitypublic class Employee {    private Integer id;    private String lastName;    private String email;    @DateTimeFormat(pattern="yyyy-MM-dd")    private Date birth;    private Date createTime;    private Department department;    @GeneratedValue    @Id    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getLastName() {        return lastName;    }    public void setLastName(String lastName) {        this.lastName = lastName;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    @Temporal(TemporalType.DATE)    public Date getBirth() {        return birth;    }    public void setBirth(Date birth) {        this.birth = birth;    }    @Temporal(TemporalType.TIMESTAMP)    public Date getCreateTime() {        return createTime;    }    public void setCreateTime(Date createTime) {        this.createTime = createTime;    }    @JoinColumn(name="DEPARTMENT_ID")    @ManyToOne(fetch=FetchType.LAZY)    public Department getDepartment() {        return department;    }    public void setDepartment(Department department) {        this.department = department;    }}

运行SSSPTest.java的testDataSoure()方法即可在数据库中生成相应的表!!!

原创粉丝点击