配置springmvc4和hibernate5

版权声明:此文章为许诗宇所写,如需转载,请写下转载文章的地址 https://blog.csdn.net/xushiyu1996818/article/details/82422699

-2 系统配置为mysql5.5 jdk1.8
-1 可以直接看
https://gitee.com/xushiyu/s_sm4_h5
0 加入jar包
https://download.csdn.net/download/xushiyu1996818/10647065
1 整体架构
这里写图片描述

2 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_3_0.xsd" 
         id="WebApp_ID" version="3.0">

   <servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/conf/springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>



  <!-- 配置spring全局监听 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/conf/beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>



</web-app>

3 springmvc

<?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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    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-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <!-- 配置注解扫描包,这里为 com.myframework.controller,
          将扫描这个包下面所有带有@Controller的类,这些类被视为Controller-->
    <context:component-scan base-package="com.ssh.controller"/>

    <!-- 开启注解扫描驱动 -->
    <mvc:annotation-driven/>

    <!-- 配置视图渲染关系,这里所有请求处理后将渲染为/WEB-INF/views下的jsp文件,
           到底是哪个jsp文件呢这就看请求处理动作到时候具体返回的是哪个文件了 -->   
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix">  
            <value>/WEB-INF/views/</value>  
        </property>  
        <property name="suffix">  
            <value>.jsp</value>  
        </property>  
    </bean>
</beans>

4 beans

<?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"
       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-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">




    <!-- 使Autowired注解生效 -->           
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

    <!-- 配置dataSource -->
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/nanny"/>
        <property name="user" value="root"/>
        <property name="password" value="1996818"/>
        <property name="initialPoolSize" value="5"/>
        <property name="maxPoolSize" value="10"/>
    </bean>

    <!-- 配置SessionFactory -->
    <bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" id="sessionFactory">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <property name="annotatedClasses"> 
            <list> 
                <value>com.ssh.po.NannyUser</value> 
            </list> 
        </property> 
   </bean>

    <!-- 开启注解扫描包  -->
    <context:component-scan base-package="com.ssh.daoImpl"></context:component-scan>
    <context:component-scan base-package="com.ssh.serviceImpl"></context:component-scan>


</beans>

5 log4j

log4j.rootLogger=WARN, Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=(%r ms) [%t] %-5p: %c#%M %x: %m%n

log4j.logger.com.genuitec.eclipse.sqlexplorer=DEBUG
log4j.logger.org.apache=WARN
log4j.logger.net.sf.hibernate=WARN

6 testcontroller

package com.ssh.controller;


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.ssh.po.NannyUser;
import com.ssh.service.NannyUserService;

@Controller
public class TestController {

    @Autowired
    private NannyUserService nannyUserService;

    public NannyUserService getNannyUserService() {
        return nannyUserService;
    }

    public void setNannyUserService(NannyUserService nannyUserService) {
        this.nannyUserService = nannyUserService;
    }

    @RequestMapping(value="/test") 
    public ModelAndView testMVC(){
        System.out.println("testMVC()");
        List<NannyUser> list = nannyUserService.getAllUser();
        System.out.println("list.size():"+list.size());
        ModelAndView modelAndView = new ModelAndView("/test");  
        modelAndView.addObject("info", "hewei");  
        return modelAndView;  
    }
}

7 service 和serviceimpl

package com.ssh.service;

import java.util.List;

import com.ssh.po.NannyUser;

public interface NannyUserService {
    public List<NannyUser> getAllUser();
    public boolean isExists(String username);
}
package com.ssh.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssh.dao.UserDao;
import com.ssh.po.NannyUser;
import com.ssh.service.NannyUserService;

@Service
public class NannyUserServiceImpl implements NannyUserService{

    @Autowired
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public NannyUserServiceImpl(){
        System.out.println("NannyUserServiceImpl()");
    }

    @Override
    public List<NannyUser> getAllUser() {
        return userDao.getAllUser();
    }

    @Override
    public boolean isExists(String username) {
        return userDao.isExists(username);
    }

}

8 dao daoimpl

package com.ssh.dao;

import java.util.List;

import com.ssh.po.NannyUser;

public interface UserDao {
   // 得到所有用户
   public List<NannyUser> getAllUser();

   // 检测用户名是否存在
   public boolean isExists(String username);
}
package com.ssh.daoImpl;

import java.util.ArrayList; 
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.ssh.dao.UserDao; 
import com.ssh.po.NannyUser; 

@Repository 
public class UserDaoImpl  implements UserDao {

    /*****注入*****/ 
    @Autowired 
    //@Qualifier("sessionFactory") 
    private SessionFactory sessionFactory;


    /*@Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }*/


    public UserDaoImpl(){ 
        System.out.println("UserDaoImpl"); 
    } 

    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    @Override 
    public List<NannyUser> getAllUser() {
        List<NannyUser> userList = new ArrayList<NannyUser>(); 
        Session session = sessionFactory.openSession(); 
        Transaction tx = session.beginTransaction(); 
        Query query = session.createQuery("from NannyUser"); 
        System.out.println(query.list().size());
        userList = query.list(); 
        tx.commit(); 
        session.close(); 
        return userList; 
        } 
    @Override 
    public boolean isExists(String username) {  
        Query query = sessionFactory.openSession()
                .createQuery("from NannyUser u where u.username = :username")
                .setParameter("username", username); 
        System.out.println(query.list().size()); 
        return query.list().size()>0?true:false; 
        } 
    }


10 po

package com.ssh.po;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="nannyuser")
public class NannyUser {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column
    private String username;

    @Column
    private String password;

    @Column
    private int gender;

    @Column
    private String name;


    public NannyUser(){

    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

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


}

猜你喜欢

转载自blog.csdn.net/xushiyu1996818/article/details/82422699