springmvc+hibernate纯注解annotation方式



一、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-

app_3_1.xsd"

version=“3.1”>

<!--监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext.xml,classpath:spring/spring-bean.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/springmvc/*</url-pattern>
    </servlet-mapping>
</web-app>

二、applicationContext.xml配置

注解方式的sessionFactoryBean配置的是AnnotationSessionFactoryBean

配置方式的sessionFactoryBean配置的是LocationSessionFactoryBean

<?xml version="1.0" encoding="UTF-8" ?>
 <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >

    <!--数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="root"></property>
        <property name="password" value=""></property>
        <property name="url" value="jdbc:mysql://localhost:3306/uplooking1?

useUnicode=true&amp;characterEncoding=UTF-8"></property>

        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <!-- <property name="initialSize" value="${connection.initialSize}"></property> -->
        <property name="maxActive" value="50"></property>
        <property name="maxIdle" value="80"></property>
        <property name="minIdle" value="20"></property>
        <property name="maxWait" value="3000"></property>
    </bean>
    <!--二级缓存-->
    <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.uplooking.club.entity</value>
            </list>
        </property>
    </bean>

    <!-- 一级缓存,使用了spring提供的工具类,包含基础的增删改查等-->
    <bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
          <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
 </beans>

三、springmvc-servlet.xml配置(这个文件要放在WEB-INF下)

<?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"
       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" >

<context:annotation-config />
<context:component-scan base-package="com.uplooking.club.controller"></context:component-

scan>

    <context:component-scan base-package="com.uplooking.club.service"></context:component-scan>
    <context:component-scan base-package="com.uplooking.club.dao"></context:component-scan>
</beans>

四、实体类配置

@Entity

@Table(name = “looker”)

public class Looker {

@GenericGenerator(name = “generator”,strategy = “native”)

@Id

@GeneratedValue(generator = “generator”)

    private Integer id;