欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate

程序员文章站 2022-05-15 13:21:37
1.hibernate译为“越冬”,指的是给java程序员带来春天,因为java程序员无需再关心各种sql了; 2.hibernate通过java类生成数据库表,通过操作对象来映射成SQL; 3.hibernate是真正意义上的ORM框架,因为他实现了对象(Object) >关系(Relation) ......

1.hibernate译为“越冬”,指的是给java程序员带来春天,因为java程序员无需再关心各种sql了;

2.hibernate通过java类生成数据库表,通过操作对象来映射成sql;

3.hibernate是真正意义上的orm框架,因为他实现了对象(object)---->关系(relation)的映射(mapping);

4.maven项目整体包结构(报错是eclipse发神经,不用管)

javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate

因为hibernate可以根据类生成表,所以只需创建数据库即可;

create database ssh;

 

该案例实现了一个用户拥有多部手机的关系

 

5.各种配置文件

(1)pom.xml

<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.xiaog</groupid>
  <artifactid>testssh</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>war</packaging>
  
  <dependencies>
      <!-- spring+springmvc*依赖包,包含spring-webmvc、spring-aop、spring-beans、
      spring-context、spring-core、spring-jcl、spring-expression、spring-web -->
      <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-webmvc</artifactid>
      <version>5.1.5.release</version>
    </dependency>
    
    <!-- spring-aop所依赖的静态代理 ,
        使用aop方式管理事务,在service方法执行前开启事务,
        方法执行后提交事务,方法执行失败回滚事务-->
    <dependency>
        <groupid>org.aspectj</groupid>
        <artifactid>aspectjrt</artifactid>
        <version>1.8.0</version>
    </dependency>
    <dependency>
        <groupid>org.aspectj</groupid>
        <artifactid>aspectjweaver</artifactid>
        <version>1.8.0</version>
    </dependency>
    
    <!-- 使用jdbctemplate中的事务实现 -->
    <dependency>
      <groupid>org.springframework</groupid>
      <artifactid>spring-jdbc</artifactid>
      <version>5.1.5.release</version>
    </dependency>
    
    <!-- 数据库方面 -->
    <dependency>
        <groupid>mysql</groupid>
        <artifactid>mysql-connector-java</artifactid>
        <version>5.1.47</version>
    </dependency>
    <dependency>
        <groupid>com.alibaba</groupid>
        <artifactid>druid</artifactid>
        <version>1.1.10</version>
    </dependency>
    
    <!-- orm框架 -->
    <dependency>
        <groupid>org.hibernate</groupid>
        <artifactid>hibernate-core</artifactid>
        <version>4.2.2.final</version>
    </dependency>

    <!-- spring对于orm框架的支持 -->
    <dependency>
        <groupid>org.springframework</groupid>
        <artifactid>spring-orm</artifactid>
        <version>4.2.4.release</version>
    </dependency>

    
    <!-- logback -->
    <dependency>
        <groupid>ch.qos.logback</groupid>
        <artifactid>logback-classic</artifactid>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupid>org.logback-extensions</groupid>
        <artifactid>logback-ext-spring</artifactid>
        <version>0.1.2</version>
    </dependency>
    <dependency>
        <groupid>org.slf4j</groupid>
        <artifactid>jcl-over-slf4j</artifactid>
        <version>1.7.12</version>
    </dependency>
    
    <!-- jsp需要 -->
    <dependency>
      <groupid>javax.servlet</groupid>
      <artifactid>servlet-api</artifactid>
      <version>3.0-alpha-1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupid>jstl</groupid>
      <artifactid>jstl</artifactid>
      <version>1.1.2</version>
    </dependency>
  </dependencies>
  
  <build>
  <defaultgoal>compile</defaultgoal>
      <plugins>
          <!-- maven插件 -->
         <plugin> 
            <groupid>org.apache.maven.plugins</groupid> 
            <artifactid>maven-compiler-plugin</artifactid>
            <version>3.1</version> 
            <configuration>
            <source>1.8</source>
            <target>1.8</target>            
                <encoding>utf-8</encoding> 
            </configuration> 
         </plugin> 
         
        <!-- tomcat插件 -->
        <plugin>
            <groupid>org.apache.tomcat.maven</groupid>
            <artifactid>tomcat7-maven-plugin</artifactid>
            <version>2.1</version>
            <configuration>
                <port>9999</port>
                <path>/testssh</path>
                <uriencoding>utf-8</uriencoding>
                <finalname>testssh</finalname>
                <server>tomcat7</server>
            </configuration>
        </plugin> 
<!--           <plugin>
          <groupid>org.eclipse.jetty</groupid>
          <artifactid>jetty-maven-plugin</artifactid>
          <version>9.3.7.v20160115</version>
        </plugin> -->
      </plugins>
  </build>
</project>

 

(2)spring-context.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:aop="http://www.springframework.org/schema/aop"
    xsi:schemalocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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"
        default-autowire="byname" default-lazy-init="true">
    <description>spring公共配置 </description>
    <!-- 1.扫描包: @repository @service @autowired @resource  -->
    <context:component-scan base-package="com.xiaog.dao,com.xiaog.service" />
    
    <!-- 2.加载配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    
    <!-- 3.配置连接池 :druid连接池 -->
    <bean id="datasource" class="com.alibaba.druid.pool.druiddatasource">
        <property name="driverclassname" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 初始化连接数量 -->
       <property name="initialsize" value="5" />
       <!-- 最大连接数 -->
       <property name="maxactive" value="100" />
       <!-- 最小连接数 -->
       <property name="minidle" value="5" />
       <!-- 配置获取连接等待超时的时间 -->
       <property name="maxwait" value="120000" />
       <!-- 超过时间限制是否回收 -->
       <property name="removeabandoned" value="true" />
       <!-- 超过时间限制多长 -->
       <property name="removeabandonedtimeout" value="1800" />
       <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
       <property name="timebetweenevictionrunsmillis" value="120000" />
       <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
       <property name="minevictableidletimemillis" value="300000" />
       <!-- 用来检测连接是否有效的sql,要求是一个查询语句 -->
       <property name="validationquery" value="select 1" />
       <!-- 申请连接的时候检测 -->
       <property name="testwhileidle" value="true" />
       <!-- 申请连接时执行validationquery检测连接是否有效,配置为true会降低性能 -->
       <property name="testonborrow" value="false" />
       <!-- 归还连接时执行validationquery检测连接是否有效,配置为true会降低性能 -->
       <property name="testonreturn" value="false" />
       <!-- 打开pscache,并且指定每个连接上pscache的大小 -->
       <property name="poolpreparedstatements" value="true" />
       <property name="maxpoolpreparedstatementperconnectionsize" value="20" />
       <property name="defaultautocommit" value="false" />
       <!-- 配置监控统计拦截的filters -->
       <property name="filters" value="stat"/>
    </bean>

    <!-- 4.spring集成hibernate  -->
    <!-- 配置hibernate的sessionfactory -->
    <bean id="sessionfactory"
        class="org.springframework.orm.hibernate4.localsessionfactorybean">
        <!-- 注入数据源 相关信息看源码 -->
        <property name="datasource" ref="datasource" />
        <!-- hibernate配置信息 -->
        <property name="hibernateproperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- 扫描hibernate注解配置的entity -->
        <property name="packagestoscan" value="com.xiaog.entity" />
    </bean>

    
    <!-- 5.配置事务管理器 -->
    <bean id="transactionmanager"
        class="org.springframework.orm.hibernate4.hibernatetransactionmanager">
        <property name="sessionfactory" ref="sessionfactory" />
    </bean>
    
    <!-- 6.配置aop -->
    <!-- 配置通知: 定位方法 -->
    <tx:advice id="txadvice" transaction-manager="transactionmanager">
        <tx:attributes>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面 -->
    <aop:config>
        <!-- 定位具体的类:完整类名,使用通配符 -->
        <aop:advisor advice-ref="txadvice" pointcut="execution(* com.xiaog.service.*.*(..))" />
    </aop:config>        
   
</beans>

 

 

 

(3)spring-mvc.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/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
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        
        <!-- 1.启动自动扫描 -->
        <context:component-scan base-package="com.xiaog.controller" />
        
        <!-- 2.注册mvc注解驱动 -->
        <mvc:annotation-driven />
        
        <!-- 3.配置静态资源    css js imgs -->
        <mvc:resources location="/resources/**" mapping="/resources"/>        
        <mvc:resources location="/webapp/static/**" mapping="/webapp/static"/>     
       
        <!-- 4.附件上传 -->   
        <!-- <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> -->    
            <!-- 默认编码 -->  
            <!-- <property name="defaultencoding" value="utf-8" /> -->    
            <!-- 文件大小最大值 -->  
            <!-- <property name="maxuploadsize" value="10485760000" />     -->
            <!-- 内存中的最大值 -->  
            <!-- <property name="maxinmemorysize" value="40960" /> -->
            <!-- 启用是为了推迟文件解析,以便捕获文件大小异常 -->
            <!-- <property name="resolvelazily" value="true"/> -->
        <!-- </bean>  -->
                 
        <!-- 5.配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.internalresourceviewresolver">
            <property name = "prefix" value="/" /><!-- 前缀 -->
            <property name = "suffix" value = ".jsp" /><!-- 后缀 -->
             <property name="viewclass" value="org.springframework.web.servlet.view.jstlview" />  
        </bean>

</beans>

 

(4)logback.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <!-- 控制台输出 -->   
    <appender name="stdout" class="ch.qos.logback.core.consoleappender">
       <!-- 日志输出编码 -->  
       <!-- <encoding>utf-8</encoding>    -->
        <layout class="ch.qos.logback.classic.patternlayout">   
             <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> 
            <pattern>%d{yyyy-mm-dd hh:mm:ss.sss} [%thread] %-5level %logger{50} - %msg%n   
            </pattern>   
        </layout>   
    </appender>   
    <!-- 日志输出级别 -->
    <root level="info"> 
        <appender-ref ref="stdout" />   
    </root> 
    <!-- 打印sql语句 -->
    <logger name="org.hibernate.type.descriptor.sql.basicbinder" level="trace" />  
    <logger name="org.hibernate.type.descriptor.sql.basicextractor" level="debug" />  
    <logger name="org.hibernate.sql" level="debug" />
     <logger name="org.hibernate.type" level="info" />
    <logger name="org.hibernate.engine.queryparameters" level="debug" />  
     <logger name="org.hibernate.engine.query.hqlqueryplan" level="debug" />
</configuration>

 

(5)jdbc.properties

jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://localhost:3306/ssh?usessl=false&amp;useunicode=true&amp;characterencoding=utf8
jdbc.username=root
jdbc.password=root

 

(6)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" version="3.0">
  <display-name>testssh</display-name>
  <!-- spring配置文件 -->
      <context-param>
        <param-name>contextconfiglocation</param-name>
        <param-value>classpath:spring-context.xml</param-value>
    </context-param>  
  <!-- spring监听器 -->
      <listener>
        <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
    </listener>
    
     <!-- 字符编码过滤器   spring web自动提供一个 -->
    <filter>
        <filter-name>encodingfilter</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>
        <init-param>
            <param-name>forceencoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingfilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
        

    <!-- spring  mvc 配置  【*控制器/前端控制器/总控】 -->
    <servlet>  
        <servlet-name>spring-mvc</servlet-name>  
        <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>  
         <!-- 配置spring mvc下的配置文件的位置和名称 -->
        <init-param>
             <param-name>contextconfiglocation</param-name>
             <param-value>classpath:spring-mvc.xml</param-value>
         </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 可以配置扩展名,*.do -->  
    <servlet-mapping>  
        <servlet-name>spring-mvc</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>
  
       <!-- 添加日志监听器 -->  
    <context-param>  
        <param-name>logbackconfiglocation</param-name>  
        <param-value>classpath:logback.xml</param-value>  
    </context-param>  
    <listener>  
        <listener-class>ch.qos.logback.ext.spring.web.logbackconfiglistener</listener-class>  
    </listener> 
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

6.实体类user和phone

package com.xiaog.entity;

import java.util.list;

import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.generationtype;
import javax.persistence.id;
import javax.persistence.joincolumn;
import javax.persistence.onetomany;
import javax.persistence.table;

@entity
@table(name="user")
public class user {
    
    @id
    @generatedvalue(strategy=generationtype.identity)
    private integer id;
    
    private string username;
    
    private string password;
    
    @onetomany(targetentity=phone.class)
    @joincolumn(name="user_id")
    private list<phone> phones;//一个用户拥有多部手机

    public integer getid() {
        return id;
    }

    public void setid(integer 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 list<phone> getphones() {
        return phones;
    }

    public void setphones(list<phone> phones) {
        this.phones = phones;
    }

    @override
    public string tostring() {
        return "user [id=" + id + ", username=" + username + ", password=" + password + ", phones=" + phones + "]";
    }
    
}

 

package com.xiaog.entity;

import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.generationtype;
import javax.persistence.id;
import javax.persistence.table;

@entity
@table(name="phone")
public class phone {
    
    @id
    @generatedvalue(strategy=generationtype.identity)
    private integer id;
    
    private string brand;//品牌

    public integer getid() {
        return id;
    }

    public void setid(integer id) {
        this.id = id;
    }

    public string getbrand() {
        return brand;
    }

    public void setbrand(string brand) {
        this.brand = brand;
    }

    @override
    public string tostring() {
        return "phone [id=" + id + ", brand=" + brand + "]";
    }

}

 

 

7.核心dao接口及其实现

package com.xiaog.core.dao;

import java.util.list;

public interface coredao<t> {
    
    int insert(t t);
    
    int delete(int id);
    
    int update(t t);
    
    t getone(int id);
    
    list<t> getlist(t t);
    
}
package com.xiaog.core.dao.impl;

import java.lang.reflect.parameterizedtype;
import java.util.list;

import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.orm.hibernate4.support.hibernatedaosupport;

import com.xiaog.core.dao.coredao;
import com.xiaog.entity.user;

//继承hibernatedaosupport 可以直接使用crud操作
public class coredaoimpl<t> extends hibernatedaosupport implements coredao<t> {
    
    private class<t> clazz;
    
    private final static logger logger = loggerfactory.getlogger(coredaoimpl.class);
    
    @suppresswarnings("unchecked")
    public coredaoimpl() {
        this.clazz =  (class<t>)((parameterizedtype) getclass().getgenericsuperclass()).getactualtypearguments()[0];
    }
    
    @override
    public int insert(t t) {
        return (int) super.gethibernatetemplate().save(t);
    }

    @override
    public int delete(int id) {
        super.gethibernatetemplate().delete(new user() {{setid(id);}});
        return 1;
    }

    @override
    public int update(t t) {
        super.gethibernatetemplate().update(t);
        return 1;
    }

    @override
    public t getone(int id) {
        return super.gethibernatetemplate().get(clazz, id);
    }

    @override
    public list<t> getlist(t t) {
        //logger.info("进入coredaoimpl......");
        return super.gethibernatetemplate().findbyexample(t);
    }

}

 

 

8.模块dao接口及其实现(只需继承核心dao即可)

package com.xiaog.dao;

import com.xiaog.core.dao.coredao;
import com.xiaog.entity.user;

public interface userdao extends coredao<user> {

}
package com.xiaog.dao;

import com.xiaog.core.dao.coredao;
import com.xiaog.entity.phone;

public interface phonedao extends coredao<phone> {

}

 

package com.xiaog.dao.impl;

import org.springframework.stereotype.repository;

import com.xiaog.core.dao.impl.coredaoimpl;
import com.xiaog.dao.userdao;
import com.xiaog.entity.user;

@repository
public class userdaoimpl extends coredaoimpl<user> implements userdao {

}
package com.xiaog.dao.impl;

import org.springframework.stereotype.repository;

import com.xiaog.core.dao.impl.coredaoimpl;
import com.xiaog.dao.phonedao;
import com.xiaog.entity.phone;

@repository
public class phonedaoimpl extends coredaoimpl<phone> implements phonedao {


}

 

 

9.service接口及其实现

package com.xiaog.service;

import com.xiaog.entity.user;

public interface userservice {
    user login(user user);
}
package com.xiaog.service.impl;

import java.util.list;

import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;

import com.xiaog.dao.userdao;
import com.xiaog.entity.user;
import com.xiaog.service.userservice;

@service
public class userserviceimpl implements userservice {

    @autowired
    private userdao userdao;
    
    private final static logger logger = loggerfactory.getlogger(userserviceimpl.class);
    
    @override
    public user login(user user) {
        //logger.info("进入userserviceimpl......");
        list<user> users = userdao.getlist(user);
        logger.info("users="+users);
        if(users!=null&&users.size()>0) {
            return users.get(0);
        }else {
            return null;
        }
    }

}

 

 

10.controller

package com.xiaog.controller;

import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;

import com.xiaog.entity.user;
import com.xiaog.service.userservice;

@controller
@requestmapping("/user")
public class usercontroller {

    @autowired
    private userservice userservice;
    
    private final static logger logger = loggerfactory.getlogger(usercontroller.class);
    
    @requestmapping(value = "/login",params= {"username","password","username!=","password!="})
    public string login(model model,user user) {
        logger.info("user(request)="+user);
        user = userservice.login(user);
        logger.info("user="+user);
        model.addattribute("user", user);
        return "result";
    }
    
}

 

 

11.jsp页面测试登录

<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>insert title here</title>
</head>
<body>
    <form action="user/login" method="post">
        <div>
            <label>username</label>
            <input type="text" name="username"/>
        </div>
        <div>
            <label>password</label>
            <input type="password" name="password"/>
        </div>
        <div>
            <input type="submit" value="登录">
        </div>
    </form>
</body>
</html>
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
    username=${user}
</body>
</html>

 

 

12.项目启动后,你会发现你的ssh数据库中多了两张表user和phone,自己在插入几条数据

user表                                                 phone表

javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate      javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate

 

13.测试

index.jsp表单中输入1001和123

javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate

result.jsp

javaweb各种框架组合案例(三):maven+spring+springMVC+hibernate

 

控制台打印

2019-06-22 09:40:24.477 [http-bio-9999-exec-9] info  com.xiaog.controller.usercontroller - user(request)=user [id=null, username=1002, password=456, phones=null]
2019-06-22 09:40:24.519 [http-bio-9999-exec-9] debug org.hibernate.sql - select this_.id as id1_1_0_, this_.password as password2_1_0_, this_.username as username3_1_0_ from user this_ where (this_.password=? and this_.username=?)
hibernate: select this_.id as id1_1_0_, this_.password as password2_1_0_, this_.username as username3_1_0_ from user this_ where (this_.password=? and this_.username=?)
2019-06-22 09:40:24.520 [http-bio-9999-exec-9] trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [1] as [varchar] - 456
2019-06-22 09:40:24.520 [http-bio-9999-exec-9] trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [2] as [varchar] - 1002
2019-06-22 09:40:24.523 [http-bio-9999-exec-9] debug org.hibernate.sql - select phones0_.user_id as user3_1_1_, phones0_.id as id1_0_1_, phones0_.id as id1_0_0_, phones0_.brand as brand2_0_0_ from phone phones0_ where phones0_.user_id=?
hibernate: select phones0_.user_id as user3_1_1_, phones0_.id as id1_0_1_, phones0_.id as id1_0_0_, phones0_.brand as brand2_0_0_ from phone phones0_ where phones0_.user_id=?
2019-06-22 09:40:24.524 [http-bio-9999-exec-9] trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [1] as [integer] - 2
2019-06-22 09:40:24.533 [http-bio-9999-exec-9] info  com.xiaog.service.impl.userserviceimpl - users=[user [id=2, username=1002, password=456, phones=[phone [id=3, brand=三星], phone [id=4, brand=小米], phone [id=5, brand=魅族]]]]
2019-06-22 09:40:24.557 [http-bio-9999-exec-9] info  com.xiaog.controller.usercontroller - user=user [id=2, username=1002, password=456, phones=[phone [id=3, brand=三星], phone [id=4, brand=小米], phone [id=5, brand=魅族]]]
2019-06-22 10:38:09.540 [http-bio-9999-exec-5] info  com.xiaog.controller.usercontroller - user(request)=user [id=null, username=1001, password=123, phones=null]
2019-06-22 10:38:09.549 [http-bio-9999-exec-5] debug org.hibernate.sql - select this_.id as id1_1_0_, this_.password as password2_1_0_, this_.username as username3_1_0_ from user this_ where (this_.password=? and this_.username=?)
hibernate: select this_.id as id1_1_0_, this_.password as password2_1_0_, this_.username as username3_1_0_ from user this_ where (this_.password=? and this_.username=?)
2019-06-22 10:38:09.550 [http-bio-9999-exec-5] trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [1] as [varchar] - 123
2019-06-22 10:38:09.550 [http-bio-9999-exec-5] trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [2] as [varchar] - 1001
2019-06-22 10:38:09.554 [http-bio-9999-exec-5] debug org.hibernate.sql - select phones0_.user_id as user3_1_1_, phones0_.id as id1_0_1_, phones0_.id as id1_0_0_, phones0_.brand as brand2_0_0_ from phone phones0_ where phones0_.user_id=?
hibernate: select phones0_.user_id as user3_1_1_, phones0_.id as id1_0_1_, phones0_.id as id1_0_0_, phones0_.brand as brand2_0_0_ from phone phones0_ where phones0_.user_id=?
2019-06-22 10:38:09.556 [http-bio-9999-exec-5] trace org.hibernate.type.descriptor.sql.basicbinder - binding parameter [1] as [integer] - 1
2019-06-22 10:38:09.563 [http-bio-9999-exec-5] info  com.xiaog.service.impl.userserviceimpl - users=[user [id=1, username=1001, password=123, phones=[phone [id=1, brand=华为], phone [id=2, brand=iphone]]]]
2019-06-22 10:38:09.570 [http-bio-9999-exec-5] info  com.xiaog.controller.usercontroller - user=user [id=1, username=1001, password=123, phones=[phone [id=1, brand=华为], phone [id=2, brand=iphone]]]

 

 

14.注意点:我个人觉得ssh框架坑实在是多,我在搭建的过程中出现了各种各样的问题,不过最恶心的问题是spring和hibernate存在版本不兼容问题,一开始使用的是spring5+hibernate5,发现报错,就将hibernate5换成4(应该没问题了吧),结果还是报错,折腾半天,发现虽然pom版本虽然换成4,但是之前dao层用到的hibernatedaosupport和spring-context.xml配置中sessionfactory还是5的版本,诶,智障问题。