使用Hibernate 如何把多个表的数据查询到一个POJO

http://blog.csdn.net/yangqicong/article/details/6910740
看了你这篇文章后,有一个疑问.

如果写SQL把 A B 表关联,查询出A 表全部字段和B表一个字段 别名为A 表里一个不被hibernate 创建到数据库表的字段,
也就是说查询出的字段都和A POJO 字段一一对应.
那么我怎么把查询结果放到A POJO 去呢?

然后发现可以尝试

 query.setResultTransformer(Transformers.aliasToBean(SM_Power.class));

但是这样写又有新问题,MYSQL 没有Long类型,但是POJO 里面有,然后hibernate就会报错.

query.addScalar("id", Hibernate.LONG);

这样指定了就不会类型错误,但是这样只能查询出一个字段,如果很多个字段那不就搞死人了.

所以以上总结后有2个方案.
1 有没有除了代码1块 的处理结果的其他方法
2 有没有和代码2块 功能相反的方法,除了指定的其他的按照 代码块1的执行转换.
不知道你们是怎么处理的,因为才接触hibernate ,所以请大伙能给个更好的方法.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
pojo中的域并不在数据库中存在,hibernate如何定义
hibernate 映射的class的变量可以定义多于数据库中的域么?pojo中的域并不在数据库中存在,hibernate如何定义 即类似 grails中class定义中的transients属性 即:pojo中的值并不在数据库中存在 如中间变量或数据 当然,多添加个BO来做没问题,再赋值,再保存。感觉没必要而已。就如grails中,直接赋予transients属性就很简单 --------------- 搜索了下,貌似无解 http://jira.codehaus.org/browse/GRAILS-4978 与我提的问题有点反方向,但解答有关: In effect, transient properties are not part of Hibernate (annotation) mapping meta data and this appears to be very reasonable, as @Transient states just that Hibernate should completely ignore a particular property. Within org.codehaus.groovy.grails.orm.hibernate.GrailsHibernateDomainClass, Grails then uses these meta data to construct the properties of the persistent class. This will result in transient properties missing from the properties collection of a persistent class For your example, transientField will not be a member of myDomainInstance.properties but can still be accessed via myDomainInstance.transientField. As the scaffolding logic uses the properties from the persistent class, it will just not see any transient property. A fix might require GrailsHibernateDomainClass to introspect the persistent class on its own and to merge those properties with the properties from the meta class - provided there is a way to add properties that are not mapped by Hibernate. A work-around might be attempted by adding missing properties within application code, but I suspect it may not be a task for the faint of heart -------------------- 嘿嘿,貌似扩展个类,再upcasting就行了,试试看。----我自言自语ing
hibernate中写hql语句查询多对多用户pojo类
查询条件是User属性和与User多对多关联的UserGroup的id和Role的id。 我开始是这样写的: String hql = "select u from User u join fetch u.userGounp ug join fetch u.role r where u.userName like '%孙悟空%' and ug.id=2 and ug.id=4 and r.id=1"; 但并没有查出来结果,而数据库中是有一条user满足条件的。 后来尝试写这样语句: String hql = "select u from User u join fetch u.userGounp ug join fetch u.role r where u.userName like '%孙悟空%' and ug.id in(2,4) and r.id=1"; 但查询的结果中,同一个id 的User信息出现了两次,哪怕使用左连接也一样查询出两条同一个id,而且满足ug.id=2或者ug.id=4的也查询出来了。 有哪位帮我解决一下吗? 我要的要求是: 1.查询条件可以使User的属性 (ps:userName) 2.满足第一个要求下查询它的用户组是满足条件(ps:userGroup.id=2 and userGroup.id=2) 3.是角色跟第二条要求一样 4.分页,分页方法我已经写好了(getListForPage(hql, fromIndex,pageSize);)
用hibernateTool生成pojo类时,多了一个 表名Id.java 的pojo,为什么
比如数据库中表为table.用hibernateTool生成pojo和hbm时,会产生一个 TableId.java和一个Table.java,为什么, 这时候如何插入数据, Table.java: public class Table implements java.io.Serializable { private TableId id; public Table() { } .... } TableId.java中有数据库中表table 的实际属性,这时怎么操作,或者哪位提供个文档,我自己去看也行. [b]问题补充:[/b] Table里只有一个Id属性. 就算new一个,也一样,还是只有一个setId的方法,没有set其它属性的方法. [b]问题补充:[/b] 首先对表示感谢.然后贴一下数据库设置. bz_id char(1) bz_name varchar(20) idx smallint CREATE TABLE [dbo].[BZ]( [bz_id] [char](1) NULL CONSTRAINT [DF_BZ_bz_id] DEFAULT (''), [bz_name] [varchar](20) NULL CONSTRAINT [DF_BZ_bz_name] DEFAULT (''), [idx] [smallint] NULL CONSTRAINT [DF_BZ_idx] DEFAULT (0) ) ON [PRIMARY] 里面没有设置主键.而且id和idx都允许空的,(因为业务要求不同,所以有两个ID.) [b]问题补充:[/b] 下面是Bz.hbm.xml [code="java"] <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2009-11-11 23:23:08 by Hibernate Tools 3.2.4.GA --> <hibernate-mapping> <class name="org.gaodi.bean.Bz" table="BZ" schema="dbo"> <composite-id name="id" class="org.gaodi.bean.BzId"> <key-property name="bzId" type="java.lang.Character"> <column name="bz_id" length="1" /> </key-property> <key-property name="bzName" type="string"> <column name="bz_name" length="20" /> </key-property> <key-property name="idx" type="java.lang.Short"> <column name="idx" /> </key-property> </composite-id> </class> </hibernate-mapping>[/code] [b]问题补充:[/b] Bz.java [code="java"] package org.gaodi.bean; // Generated 2009-11-11 23:23:08 by Hibernate Tools 3.2.4.GA /** * Bz generated by hbm2java */ public class Bz implements java.io.Serializable { private BzId id; public Bz() { } public Bz(BzId id) { this.id = id; } public BzId getId() { return this.id; } public void setId(BzId id) { this.id = id; } } [/code] BzId.java [code="java"] package org.gaodi.bean; // Generated 2009-11-11 23:23:08 by Hibernate Tools 3.2.4.GA /** * BzId generated by hbm2java */ public class BzId implements java.io.Serializable { private Character bzId; private String bzName; private Short idx; public BzId() { } public BzId(Character bzId, String bzName, Short idx) { this.bzId = bzId; this.bzName = bzName; this.idx = idx; } public Character getBzId() { return this.bzId; } public void setBzId(Character bzId) { this.bzId = bzId; } public String getBzName() { return this.bzName; } public void setBzName(String bzName) { this.bzName = bzName; } public Short getIdx() { return this.idx; } public void setIdx(Short idx) { this.idx = idx; } public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof BzId)) return false; BzId castOther = (BzId) other; return ((this.getBzId() == castOther.getBzId()) || (this.getBzId() != null && castOther.getBzId() != null && this.getBzId().equals( castOther.getBzId()))) && ((this.getBzName() == castOther.getBzName()) || (this .getBzName() != null && castOther.getBzName() != null && this.getBzName() .equals(castOther.getBzName()))) && ((this.getIdx() == castOther.getIdx()) || (this.getIdx() != null && castOther.getIdx() != null && this.getIdx().equals( castOther.getIdx()))); } public int hashCode() { int result = 17; result = 37 * result + (getBzId() == null ? 0 : this.getBzId().hashCode()); result = 37 * result + (getBzName() == null ? 0 : this.getBzName().hashCode()); result = 37 * result + (getIdx() == null ? 0 : this.getIdx().hashCode()); return result; } } [/code]
Spring+Hibernate多表关联的问题
1.多表关联配置了相关many-to-many 并且在pojo里写了Set 为什么每次添加中间表里都没有数据? 2.是不是添加的控制器如果用spring的simpleFormController继承的话 关联的时候一些外键没法格式化? 因此造成中间表里没有数据? 3.如果中间表里还需要附加其他字段 该怎么办? 比如 student---sid,sname teacher---tid,tname course----sid,tid 我在student的form中有一项是teacher的列表select 在student和teacher的pojo里都有Set对方的代码 添加时用的controller是继承自simpleFormController(其中有一项commandclass我设为了student的pojo) 使用一个共用的DAO,代码段为: [code="java"] ...handleRequest(...request,..response,Command cmd,...)... Student student = (Student)cmd; CommonDAO.saveOrUpdate(student);//如果要添加成功,是否需要在此处加上插入course的语句?问题是,该怎么加,因为teacher的id也包含在cmd里边了吧 [/code] 现在student能进库而course依然为空,为何? [b]问题补充:[/b] 关键之处在于和spring整合 没有人遇到过这个问题吗?
hibernate 关联保存 的困惑
两个POJO,Riji(日记) 和Rijipinglun (日记评论) [code="java"] Riji riji=findRiji(id); Rijipinglun rijipinglun=new Rijipinglun(); [color=red]riji.getRijipingluns().add(rijipinglun); rijipinglun.setRiji(riji);[/color] save(rijipinglun); [/code] 问题就在两句红的那,如果我只写第二句,那么hibernate会发出两个语句,一个查询日记,一个保存评论, 可是加了红色 第一句,那么hibernate会多发出一个查找相关评论的语句 hibernate视乎推荐第二个,为什么,这样好像效率更低 [b]问题补充:[/b] [code="java"]Riji riji=findRiji(id); Rijipinglun rijipinglun=new Rijipinglun(); riji.getRijipingluns().add(rijipinglun); rijipinglun.setRiji(riji); save(rijipinglun); [/code] 可能问题还没问清楚。 我觉得下面这句可有可无,因为通常多端是控制端,并且一般都是,而且设置了级联 保存更新,而下面的句子平白的多了一次查询,及查询日记的所有评论记录。 [color=red]riji.getRijipingluns().add(rijipinglun);[/color] [b]问题补充:[/b] 蔡华江 lovewhzlq 都讲到了关联双向。我现在考虑了一下: 事实上 riji.getRijipingluns().add(rijipinglun); 不会对持久化有影响。 当然有时候估计操作方便,可能还会用到,比如同时要持久化多个 riJiPinglun,那么没有前面那句话,就得一个一个去save. 对于hibernate 一级缓存而言(个人认为一级缓存作用不大),持久化了riJiPinglun之后紧接着就会关闭session,而在另一个session中取得日记所属日记评论。 另外大部分时候调用riji.getRijipingluns()都会导致加载所有的数据,通常我们会使用过滤或者hql限制取得条件。
请教S2SH整合,hibernate的多对多关系!联合主键查询问题!求指教!!!!!!!!!
goods商品表 int goodsid 自增,主键 varchar goodsName varchar goodsAdress relation中间表 int goods_id 主键 (无自增) 是goods表的外键 int stor_id 主键(无自增)是stor表的外键 int goodsNum int price stor 仓库表 int stor_id 主键,自增 varchar storName varchar storAdress S2SH整合 这3个Pojo类和hibernate.hbm.xml是逆向自动生成的。 怎么查询出3个表中所有的数据同时传递到JSP页面。越详细越好!注重方法! 谢谢。。求指教 !!
Hibernate不能插入数据但可以查找数据(异灵事件!)
发这种没有技术含量的问题,也是出于无奈。自己倒腾了1天多了还是没有结果。 希望各位大人海涵。 问题是这样的 我使用的是 MyEclipse 6.5GA 1·建立一个普通的Java项目 2·加入Hibernate支持(MyEclipse->Porject Capabilities) (1) 选择Hibernate3.2 (2) 创建SessionFactory 3·对数据库中的表建立反向工程 (1) 生成*.hbm.xml文件 (2) 生成Pojo类 (3) 生成BasicDAO (4)id增长类型选择native(mySQl中已经配置了自增长) 4·创建Main函数 [code="java"] package hibernate; public class Main { /** * @param args */ public static void main(String[] args) { NewTableDAO dao = new NewTableDAO(); NewTable test = new NewTable(); test.setName("Help"); dao.save(test); System.out.println("Finished"); } } [/code] 运行结果: Hibernate: insert into test.`new table` (name) values (?) Finished 但是数据库中没有插入任何数据! 不过要是运行自动生成的Dao中的任何查找功能都是没有问题的。 自动生成的Dao如下(部分): [code="java"] public class NewTableDAO extends BaseHibernateDAO { private static final Log log = LogFactory.getLog(NewTableDAO.class); // property constants public static final String NAME = "name"; public void save(NewTable transientInstance) { log.debug("saving NewTable instance"); try { getSession().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } [/code] 我在web项目中 及加入Hibernate、Spring(Hibernate不建立SessionFactory,使用Spring的) 在Juit中跑相同代码没有任何问题。 请问我这个问题出在哪里了 谢谢。
关于hibernate建表的疑惑
[size=medium]据说实际过程中都是先建表再建类,那如果实体类之间存在一对多等单向或双向的关联,难道这时是自己直接在数据库表中定义外键?(这种关系在数据库中不是很清晰啊)然后再反向工程生成的类中的属性逻辑还对吗? 实际中如果是先建表,然后反向工程生成实体类?一般应该不能一步到位啊,如果我想要在一个实体类中有Set<T>用于一对多,反向工程之后能得到这种属性吗? 以前就算是简单的POJO都要修改映射文件,如果映射文件改了,对应的数据库也会因此发生改变吗? 有实际开发经验的谈谈,谢了。[/size]
Hibernate查询优化
最近做了一个小东东,给财务专门处理数据的。但是数据量太大,连接表太多,导致性能不佳。所以想优化hibernate代码。 我有一个POJO是这样的。 ProfCostcenter.class: [code="java"] private Long id; private ProfCreatnew profCreatnew; private String costCenterno; private String deptName; private String deptHead; private String division; private String location; private Short status; private Short pass; private Set ProfConstcentertotals= new HashSet(0); private Set profAccounts = new HashSet(0); private Set profGroupTotals = new HashSet(0); .............get set方法[/code] 对应的hbm.xml是这样的: [code="java"] <hibernate-mapping> <class name="com.profplan.model.ProfCostcenter" table="prof_costcenter" schema="dbo" catalog="whirlpool"> <id name="id" type="java.lang.Long"> <column name="id" /> <generator class="native" /> </id> <many-to-one name="profCreatnew" class="com.profplan.model.ProfCreatnew" fetch="select" lazy="false"> <column name="yearId" /> </many-to-one> <property name="costCenterno" type="java.lang.String"> <column name="costCenterno" length="50" /> </property> <property name="deptName" type="java.lang.String"> <column name="deptName" length="50" /> </property> <property name="deptHead" type="java.lang.String"> <column name="deptHead" length="50" /> </property> <property name="division" type="java.lang.String"> <column name="division" length="50" /> </property> <property name="location" type="java.lang.String"> <column name="location" length="50" /> </property> <property name="status" type="java.lang.Short"> <column name="status" /> </property> <property name="pass" type="java.lang.Short"> <column name="pass" /> </property> <set name="profAccounts" inverse="true" lazy="true"> <key> <column name="costcenterid" /> </key> <one-to-many class="com.profplan.model.ProfAccount" /> </set> <set name="profGroupTotals" inverse="true" lazy="false"> <key> <column name="costcenterId" /> </key> <one-to-many class="com.profplan.model.ProfGroupTotal" /> </set> <set name="ProfConstcentertotals" inverse="true" lazy="false"> <key> <column name="costcenterId" /> </key> <one-to-many class="com.profplan.model.ProfConstcentertotal" /> </set> </class> </hibernate-mapping> [/code] 请注意里面有好几个为lazy="false"。 因为我有的时候需要这样查询。 但是,不是所有的时候都要去查询,有的时候我仅仅需要根据ID查询 ProfCostcenter.class 的一个对象, 但这个时候每次都去查找与之相关联的其他表的数据,但我不想这样,可我 又不能去更改为lazy="true"。 请问我如何处理?如何写代码。 [b]问题补充:[/b] 我是这样写的,但是居然还是执行了很多条语句。 [code="java"] String sql="select * from whirlpool.dbo.prof_costcenter c where c.id=?"; Query q=this.getSession().createSQLQuery(sql).addEntity(ProfCostcenter.class); q.setString(0, costcenterId); q.setMaxResults(1); ProfCostcenter costcenter=(ProfCostcenter) q.uniqueResult(); [/code] 是我写的不对还是有什么问题? [b]问题补充:[/b] [code="java"] Hibernate: select top 1 * from prof_costcenter c where c.id=? java.lang.ClassCastException: [Ljava.lang.Object; at com.profplan.controller.service.impl.FillProfPlanServiceImpl.getProfplan(FillProfPlanServiceImpl.java:72) at com.profplan.controller.action.FillProfPlanAction.createProfPlan(FillProfPlanAction.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [/code] [b]问题补充:[/b] Sorry,最开始的写法是这样的,后来出错了,我就换了。 [code="java"] List<ProfCostcenter> listc=q.list(); ProfCostcenter costcenter=(ProfCostcenter) listc.get(0); [/code] 这个对吗?但还是报错哦。类型转化错误,跟上面一样。
请教:Spring + Hibernate 无法将数据写入数据库?
Spring + Hibernate 无法将数据写入数据 **请教: 通过Junit单元测试Service可以将数据写入数据库;但部署访问却无法向数据库写入数据。** 1 环境: Spring 3.1.2 Hibernate 4.1.4 Jdk1.6 2 配置: 2.1 Web.xml <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context Loader --> <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> <!--Dispathcer Servlet --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Spring MVC Servlet 拦截.do结尾的请求--> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Filter 定义 --> <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> <!--openSessionInView--> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 2.2 applicationContext.xml <!--扫描并自动装配 --> <context:annotation-config /> <context:component-scan base-package="com.sp" /> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:application.properties</value> </property> </bean> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- Connection Pooling Info --> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="defaultAutoCommit" value="false" /> <!-- 连接Idle一个小时后超时 --> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <property name="minEvictableIdleTimeMillis" value="3600000" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.connection.autocommit">true</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.sp.dao</value> <value>com.sp.entity.dict</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" abstract="false" lazy-init="default" autowire="default"> <property name="sessionFactory"><ref bean="sessionFactory" /></property> </bean> <!-- 事务管理配置 --> <!--<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" read-only="false" propagation="REQUIRED" /> <tx:method name="get*" read-only="false" propagation="REQUIRED" /> <tx:method name="add*" read-only="false" propagation="REQUIRED" /> <tx:method name="update*" read-only="false" propagation="REQUIRED" /> <tx:method name="delete*" read-only="false" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor pointcut="execution(* com.sp.service.*.*(..))" advice-ref="txAdvice"/> </aop:config> </beans> 2.3 spring-mvc.xml <!-- 启动注解驱动的SpringMVC功能,注册请求URL和注解POJO类方法的映射 --> <mvc:annotation-driven /> <!-- 自动扫描且只扫描@Controller --> <context:component-scan base-package="com.sp.web" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <context:component-scan base-package="com.sp.dao.impl"></context:component-scan> <context:component-scan base-package="com.sp.service.impl"></context:component-scan> <mvc:default-servlet-handler /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加后缀。定义JSP --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 支持上传文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> 2.4 hibernate.cfg.xml <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <property name="connection.username">root</property> <property name="connection.password">password</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect <property name="dialect">org.hibernate.dialect.MySQLDialect</property> --> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>--> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup--> <property name="hbm2ddl.auto">update</property> <property name="javax.persistence.validation.mode">none</property> 3 代码: 3.1 Service层: @Component("userService") public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Autowired private FormValidator validator; @Override public String userLogin(UserModel user) { String password = userDao.userLogin(user); if(password.equals(user.getPassword())){ return "sucess"; }else{ return "false"; } } @Override public List<ProjectType> getAllProjectTypes(){ List<ProjectType> projectType = userDao.getAllProjectTypes(); return projectType; } @Override public String addProjectType(@ModelAttribute("newProjectType")ProjectType projectType,BindingResult result,SessionStatus status){ validator.validate(projectType, result); if(result.hasErrors()){ return "newProjectType"; } userDao.save(projectType); //status.setComplete(); return "redirect:showProjectTypes.do"; } //添加《项目类型》 @Override public String addProjectType(ProjectType projectType) { userDao.save(projectType); return "redirect:showProjectTypes.do"; } } 3.2 Control层 @Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @Autowired private FormValidator validator; @RequestMapping(value = "/login",method=RequestMethod.POST) protected String handle(UserModel user,BindingResult result, Model model){ String flag = userService.userLogin(user); System.out.println("flag:" + flag); if ("sucess".equals(flag)){ return "sucess"; } return "test1"; } @RequestMapping(value = "/showProjectTypes") protected ModelAndView getAllProjectTypes(){ ModelAndView mav = new ModelAndView("showProjectTypes"); List<ProjectType> projectTypes = userService.getAllProjectTypes(); mav.addObject("SEARCH_PROJECTTYPE_RESULTS_KEY", projectTypes); return mav; } @RequestMapping(value = "/addProjectType",method=RequestMethod.GET) protected ModelAndView newProjectTypeForm(){ ModelAndView mav = new ModelAndView("newProjectType"); ProjectType projectType = new ProjectType(); mav.getModelMap().put("newProjectType", projectType); return mav; } @RequestMapping(value = "/saveProjectType",method=RequestMethod.POST) protected String addProjectType(@ModelAttribute("newProjectType")ProjectType projectType,BindingResult result,SessionStatus status){ //return userService.addProjectType(projectType, result, status); return userService.addProjectType(projectType); } } 3.3 DAO层 @Component("userDao") public class UserDaoImpl implements UserDao{ @Autowired private SessionFactory sessionFactory; public ProjectType getById(int id) { return (ProjectType) sessionFactory.getCurrentSession().get(ProjectType.class, id); } @Override public String userLogin(UserModel user) { return "123456"; } //获取数据 @Override @SuppressWarnings("unchecked") public List<ProjectType> getAllProjectTypes() { Criteria ceriteria = sessionFactory.getCurrentSession().createCriteria(ProjectType.class); return ceriteria.list(); } //新增数据; @Override // @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public int save(ProjectType projectType) { System.out.println("save:---"); System.out.println(projectType.getIndex()); System.out.println(projectType.getProjectType()); System.out.println(projectType.getProjectTypeAbbr()); return (Integer) sessionFactory.getCurrentSession().save(projectType); } }
hibernate 延迟加载异常(无法获取lazy对象)
我在jpa中有两个对象:hospitalization与patient,两个对象之间是多对一双向关联,全部都加上lazy 然后在查询的时候使用left join fetch 来加载patient service如下: [code="java"] try{ String sql="select h from Hospitalization as h left join fetch h.patient where h.code=?1"; Query query=entityManager.createQuery(sql); query.setParameter(1, code); return (Hospitalization) query.getSingleResult(); }catch(Exception e){ e.printStackTrace(); return null; } [/code] 这样返回的hospitalization这个对象无法获取patient这个属性,报no session异常: [code="java"] 2010-03-24 10:40:22,343 [main] ERROR org.hibernate.LazyInitializationException:19 - could not initialize proxy - no Session org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:166) [/code] 如果我在service上面用 [code="java"] Hospitalization h=(Hospitalization) query.getSingleResult(); // System.out.println(h.getPatient().getPatientName()); return h; [/code] 这样来写可以get到,但是这样hibernate又从数据库查询了一次,明明在调用left join fetch 这个sql的时候已经搜索出来了patient值(在sqlserver上面执行可以看到)但是他却不会组装成对象关联,返回给client的时候数据就又丢失了,请问这样的问题该怎么解决,这种情况只有个别类会遇到,完全一样的写法有些类就没问题
hibernate级联的问题,大家帮忙解决哈~~
我现在有一张用户表 CREATE TABLE `UserInfo` ( userId INT NOT NULL AUTO_INCREMENT, USERNAME VARCHAR(30) NOT NULL, PASSWORD VARCHAR(30) NOT NULL, PID INT NULL , GID INT NULL , PRIMARY KEY (userId) )ENGINE=InnoDB DEFAULT CHARSET=gb2312; 一张角色表 CREATE TABLE `ROLE` ( ROLEID INT NOT NULL AUTO_INCREMENT, ROLENAME VARCHAR(20) NOT NULL, PRIMARY KEY(ROLEID) )ENGINE=InnoDB DEFAULT CHARSET=gb2312; 还有一张员工角色连接表,里面分别是用户的ID和角色的ID CREATE TABLE `ConUserRole` ( ConUserRoleID INT NOT NULL AUTO_INCREMENT, userId INT NOT NULL, roleId INT NOT NULL, PRIMARY KEY (ConUserRoleID) )ENGINE=InnoDB DEFAULT CHARSET=gb2312; 现在我新建一个用户,在页面列出所有的角色名称,注册时可以选择多个角色,那么就需要在上面中间表中插入一行数据,但是现在中间表插不了 我的hibernate.xml文件是这样的 UserInfo.hbm.xml文件中存储的是角色中间表的set集合 <set name="conuserroles" inverse="true" table="conuserrole" lazy="false"> <key> <column name="userId" not-null="true" /> </key> <many-to-many class="com.act.dao.pojo.Role" column="roleId" /> </set> 角色Role.hbm.xml <set name="conuserroles" inverse="true" table="conuserrole" lazy="false"> <key> <column name="roleId" not-null="true" /> </key> <many-to-many class="com.act.dao.pojo.Userinfo" column="userId" /> </set> 员工角色连接表 Conuserrole.hbm.xml <many-to-one name="userinfo" class="com.act.dao.pojo.Userinfo" fetch="select"> <column name="userId" not-null="true" /> </many-to-one> <many-to-one name="role" class="com.act.dao.pojo.Role" fetch="select"> <column name="roleId" not-null="true" /> 三者之间的关联关系是这样的, 我该加什么属性或设置什么地方才能实现我在添加员工的时候也能在员工角色连接表中增加一行数据? </many-to-one> [b]问题补充:[/b] 麻烦你告诉我第二种方法该怎么配置映射文件呢? 谢谢了!!!
ssh架构的最佳实践
ssh架构可以说是目前比较流行的一个架构了,但是我接触到的ssh架构的系统,不管是实际项目中的,还是网上的例子,总是存在 各种各样的弊端。在此开一个讨论贴,希望大家提出宝贵意见。 先提出一写问题,用以抛砖引玉 hibernate关联问题 a 以订单主表和明细为例,如果在映射的时候设置为双向的一对多,那么set的lazy肯定要为true,这样就引出了session何时关闭的问题 (1).opensession in view 这个在网上最常见,因为比较方便,并且可以在view层去访问延迟加载的集合,可是当数据量和并发量大的时候可能会出现数据库连接不能及时释放的问题 (2)service层pojo-->vo,关闭session 需要多建很多vo对象,并且要处理pojo->vo的转换,感觉麻烦了一些 b 系统架构时不用关联,在程序中手动处理关联关系 也有很多人推荐这样做,这样性能会好一些,灵活性强一些,编程的工作就要麻烦一些,既然我们用了hibernate,就要利用他的方便性,不用关联真的合适吗?
hibernate配置List的问题
[code="java"] //产品分类 public class AykjProducterSort implements Serializable{ private int id; private String sortname; //产品分类名 private AykjProducterSort aykjParentPid; //父分类 private Set aykjChildrenId; //子分类 private List< :wink: AykjProperties> producterSortProperties; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSortname() { return sortname; } public void setSortname(String sortname) { this.sortname = sortname; } public List getProducterSortProperties() { return producterSortProperties; } public void setProducterSortProperties(List producterSortProperties) { this.producterSortProperties = producterSortProperties; } public AykjProducterSort getAykjParentPid() { return aykjParentPid; } public void setAykjParentPid(AykjProducterSort aykjParentPid) { this.aykjParentPid = aykjParentPid; } public Set getAykjChildrenId() { return aykjChildrenId; } public void setAykjChildrenId(Set aykjChildrenId) { this.aykjChildrenId = aykjChildrenId; } } //产品属性 public class AykjProperties implements Serializable { private int id; private String name; //商品属性 private String description; //属性描述 private AykjProperties aykjparent; //父 private Set aykjchildren; //子 public int getId() { return id; } public void setId(int 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; } public AykjProperties getAykjparent() { return aykjparent; } public void setAykjparent(AykjProperties aykjparent) { this.aykjparent = aykjparent; } public Set getAykjchildren() { return aykjchildren; } public void setAykjchildren(Set aykjchildren) { this.aykjchildren = aykjchildren; } } //AykjProducterSort 配置文件 <id name="id"> <generator class="native"/> </id> <property name="sortname"/> <many-to-one column="aykjParentPid" name="aykjParentPid"/> <set inverse="true" name="aykjChildrenId"> <key column="aykjParentPid"/> <one-to-many class="com.aykj.c2c.pojo.AykjProducterSort"/> </set> <list name="producterSortProperties" table="aykj_c2c_producterSortProperties"> <key column="id"/> <index column="position"/> <element type="string" column="properties" not-null="true"/> </list> //AykjProperties 配置文件 <id name="id"> <generator class="native"/> </id> <many-to-one column="aykjParentPid" name="aykjparent"/> <set inverse="true" name="aykjchildren"> <key column="aykjParentPid"/> <one-to-many class="com.aykj.c2c.pojo.AykjProperties"/> </set> <property name="name"/> <property name="description"/> [/code] 请问这样配置对不对 我要达到这样的目标: 因为一个产品可能有多少个属性 AykjProducterSort List<AykjProperties> producterSortProperties里面就要装下很多的AykjProperties (产品属性) 谢谢大家,帮我看下...... [b]问题补充:[/b] 火星,谢谢你,不过我那样设计是因为了好查数据的,我做的这个是商城可能有多要查询的地方..我现在也在考虑到底怎么设计数据. [b]问题补充:[/b] private String description; //属性描述 我把属性设计成树,是因为属性里面还有属性的, 比如说一个属性,年代 年代里面还有五十年代 六十年代等等的 [b]问题补充:[/b] AykjProperties 这个类能不能分开,不和别的类有关系,别的类引用他就行了....
多数据源对配置文件中sessionfactory中的packagesToScan的疑惑
``` <bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSourceOne" /> <property name="packagesToScan"> <list> <value>com.*.log.pojo*</value><!-- 修改 --> </list> </property> <property name="hibernateProperties" ref="hibernateProperties" /> </bean> ``` ``` <bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSourceTwo" /> <property name="packagesToScan"> <list> <value>com.*.pojo*</value> </list> </property> <property name="hibernateProperties" ref="hibernateProperties" /> </bean> ``` > http://blog.csdn.net/wangpeng047/article/details/8866239 我在看了这篇文章之后想将我的多个数据源的切换模式改成这样的模式 ![图片说明](https://img-ask.csdn.net/upload/201604/27/1461723942_992149.jpg) 也就是将两个sessionfactory写成一个!! 但是写到packagesToScan的时候有一个疑惑,我的两个sessionfactory内扫描的是不同的包下的,如果我写在一起是否可以如下 ``` <bean id="dynamicDataSource" class="com.core.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="dataSourceOne" key="dataSourceOne"></entry> <entry value-ref="dataSourceTwo" key="dataSourceTwo"></entry> </map> </property> <property name="defaultTargetDataSource" ref="dataSourceOne"> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dynamicDataSource" /> <property name="packagesToScan"> <list> <value>com.*.pojo*</value> <value>com.*.log.pojo*</value> </list> </property> <property name="hibernateProperties" ref="hibernateProperties" /> </bean> ``` 是否会出问题,我好纠结啊,求大神解答
关于用户自定义字段
<p>      最近工作上遇到了这样一个问题,就是关于系统用户可对系统预置管理对象进行属性的自定义。在数据库中的体现就是用户可以追加字段。</p> <p>      为此看了不少相关的资料,发现遇到这一问题的人不在少数,但可供参考的解决方案却不是很多。其实包括很多比我资深的师兄师长们都对这一需求是抵触的,认为这是一个不可接受需求,认为能有这样的需求,完全是在需求的分析上做的不够彻底。他们这样的一些看法我本人也是赞同的。但有时我们也不得不承认这样的需求在某种情况下的存在是有其理由的。就拿我们公司的情况来说,一个系统的后台管理,比如说注册用户的管理,你可以尽量地为用户考虑周全,预置很多的属性,但怎么做,也不可以满足所有用户的需求,有的用户需要手机号,有的需要工作证件号码,有的是多个邮箱等等。所以在这增加一个用户自定义的功能是很必要的。</p> <p>      对于这个问题的解决,我在看过相关资料后,发现主要有以下三种方案:</p> <p>      1)系统的数据库操作使用JDBC的方式,在POJO中,用一个MAP来对增加的字段进行操作。在这个思路的基础上,可以考虑整个POJO的对象里,只有一个MAP,它来保存所有字段名称和值。</p> <p>      2)上面那种是不采用ORM的方式,采用ORM的好处我想不必多说,而我最看重的就是它对跨数据库的良好支持。我这里只说说HIBERNATE,HIBERNATE已经有了动态加裁新增字段的功能。缺点是实现起来比较麻烦,还有就是它是通过MAP得到新增字段的值的,在考虑到与页面显示,与STRUTS的结合并不好实现。</p> <p>      3)就是预留一个TEXT的字段,然后,用"符号"区分,将区分的内容,和列名:值写出来,最后显示的时候用Split进行分开。</p> <p>     我最后是采用了第三次方式,其实在这也只是大概的提一下,给正遇到该问题的人提供一些参考,具体的实现在这就不提了。哪位大侠有更好的方案,欢迎赐教!</p><br/><strong>问题补充</strong><br/><div class="quote_title">cuiyi.crazy 写道</div><div class="quote_div">KEY-Value表是值得推荐使用的</div><br />目前的方式是除了固有字段外,自定义字段都是用XML文件来保存,自定义字段的定义也是通过XML文件来描述。这是我们头让这么弄得。其实哪种方式都能走的通,只是实现的复杂程度,和使用的方便与否。<br/><strong>问题补充</strong><br/><div class="quote_title">cuiyi.crazy 写道</div><div class="quote_div"> <br />KEY-Value表是值得推荐使用的 <br /></div> <br />这方式考虑过,不过这种方式,当有一定数量的记录时。会不会这个保存KEY-Value的表的记录数过于庞大呢?
hibernate4.2.21 延迟加载异常 了解的麻烦看一下
是哪里出问题了? 实体类 Customer package cn.hiber.entity.m2o; public class Customer { private Integer customerId; private String customerName; 省略get()、set()方法...... } 实体类 Order package cn.hiber.entity.m2o; public class Order { private Integer orderId; private String orderName; private Customer customer; 省略get()、set()方法...... } **映射文件 Customer.hbm.xml** <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.hiber.entity.m2o"> <class name="Customer" table="CUSTOMER"> <id name="customerId" column="CUSTOMER_ID"> <generator class="native" /> </id> <property name="customerName" column="CUSTOMER_NAME"></property> </class> </hibernate-mapping> **映射文件 Order.hbm.xml** <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.hiber.entity.m2o"> <class name="Order" table="ORDERS"> <id name="orderId" column="ORDER_ID"> <generator class="native" /> </id> <property name="orderName" column="ORDER_NAME"></property> <!-- 单向多对一 --> <many-to-one name="customer" class="Customer" column="CUSTOMER_ID"></many-to-one> </class> </hibernate-mapping> **核心配置文件** <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库配置信息(必须) --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hiber</property> <property name="connection.username">zhou</property> <property name="connection.password">123456</property> <!-- hibernate配置信息(可选) --> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <!-- 生成表的策略,无则创建,有则更新 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 方言 --> <!-- 设置hibernate的事务隔离级别 --> <!-- <property name="connection.isolation">2</property> --> <!-- 删除对象后,使其OID置为null --> <property name="use_identifier_rollback">true</property> <!-- 配置c3p0数据源 --> <property name="c3p0.max_size">10</property> <property name="c3p0.min_size">5</property> <property name="c3p0.acquire_increment">2</property> <!-- 连接对象被消耗完时,一次获取多少个数据库连接 --> <property name="c3p0.timeout">2000</property> <!-- 连接对象多长时间没被使用后,就该被销毁 --> <property name="c3p0.idle_test_period">2000</property> <!-- 表示连接池检测线程多长时间检测一次连接池内连接对象是否超时 --> <property name="c3p0.max_statements">10</property> <!-- 缓存statement 对象的数量 --> <!-- 引入映射文件 --> <mapping resource="cn/hiber/entity/m2o/Customer.hbm.xml"/> <mapping resource="cn/hiber/entity/m2o/Order.hbm.xml"/> </session-factory> </hibernate-configuration> 测试类 package cn.hiber.entity.m2o; public class hiberDaoTest { static SessionFactory sessionFactory = null; static Session session = null; static Transaction transaction = null; @Before public void init() { Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); session = sessionFactory.openSession(); transaction = session.beginTransaction(); } @After public void destory() { transaction.commit(); session.close(); sessionFactory.close(); } //使用get()方法或设置lazy=“false”时,无异常 @Test public void testLoad() { Customer customer = (Customer) session.load(Customer.class, 1); }; } ## 请问为什么异常?谢谢了 异常 java.lang.ClassCastException: cn.hiber.entity.m2o.Customer_$$_javassist_0 cannot be cast to javassist.util.proxy.Proxy at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxy(JavassistLazyInitializer.java:148) at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.getProxy(JavassistProxyFactory.java:73) at org.hibernate.tuple.entity.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:758) at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:4419) at org.hibernate.event.internal.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:334) at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:260) at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152) at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1053) at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:173) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.getReference(SessionImpl.java:2426) at org.hibernate.internal.SessionImpl.load(SessionImpl.java:941) at cn.hiber.entity.m2o.hiberDaoTest.testLoad(hiberDaoTest.java:35) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 请问为什么异常?谢谢了
hibernate中单向多对一的关联疑惑(管理员勿将帖子转到问答,那里不好交流)
本人初学hibernate,希望大家多多指教! <br />我假设person和card的多对一的关系(实际这两者的关系不是多对一) <br />他们对应的映射文件是: <br /><strong>person.hbm.xml</strong> <br /><pre name="code" class="xml"><!----> &lt;hibernate-mapping schema="hibernatequickuse"&gt; &lt;class name="mypack.person" table="person"&gt; &lt;id name="id" column="id" type="java.lang.Integer"&gt; &lt;generator class="identity"&gt; &lt;/id&gt; &lt;property name="name" column="name" type="java.lang.String" not-null="true"&gt;&lt;/property&gt; &lt;many-to-one name="mycard" column="card_id" class="mypack.Card" cascade="all"&gt;&lt;/many-to-one&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </pre> <br /><strong>Card.hbm.xml</strong> <br /><pre name="code" class="xml"><!----> &lt;hibernate-mapping&gt; &lt;class name="mypack.Card" table="card"&gt; &lt;id name="id" column="ID" type="java.lang.Integer"&gt; &lt;generator class="identity"&gt; &lt;/id&gt; &lt;property name="name" column="NAME" type="java.lang.String" not-null="true"&gt;&lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </pre> <br />假设当我们先把many-to-one中的cascade属性值去掉,改为: <br /><pre name="code" class="xml">&lt;many-to-one name="mycard" column="card_id" class="mypack.Card"&gt;&lt;/many-to-one&gt; </pre> <br />然后执行如下代码: <br /><pre name="code" class="java"> person p=new person(); p.setName("jack01"); Card c=new Card(); c.setName("card0031"); p.setMycard(c); Session session=null; Transaction tran=null; try{ session=factory.openSession(); tran=session.beginTransaction(); session.save(p);//为什么是update,而不是Insert tran.commit(); 。。。。。。。 </pre> <br />执行结果本来应该会报一个异常,因为只持久化了person对象。而card是临时对象,所以当hibernate自动清理缓存中的持久化对象时会发现p引用了c临时对象,而在person表中对应的card_id字段值为0,这说明person持久化对象的状态和数据库的记录不一致,所以应该会抛出异常!但是它却并没有抛出异常, <br />而是插入了一条person记录,其card_id=0这是为什么??? <br /><pre name="code" class="sql">Hibernate: insert into hibernatequickuse.person (name, card_id) values (?, ?)</pre> <br />当我把cascade="all"加上去之后,本来这时应该是先插入card记录,然后插入Person记录。但结果却是: <br /><pre name="code" class="sql">Hibernate: insert into hibernatequickuse.person (name, card_id) values (?, ?) Hibernate: update card set NAME=? where ID=? </pre> <br />而且抛出异常,表示无法更新!这又是为什么???????? <br /><pre name="code" class="java">Exception executing batch: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 </pre> <br /><strong>表结构如下:</strong> <br /><pre name="code" class="sql">-- -- 表的结构 `card` -- CREATE TABLE `card` ( `id` int(11) NOT NULL auto_increment, `name` varchar(20) collate latin1_general_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=7 ; CREATE TABLE `person` ( `id` int(11) NOT NULL auto_increment, `name` varchar(32) collate latin1_general_ci NOT NULL, `card_id` int(11) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `card_id` (`card_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4 ; </pre> <br /><strong>POJO如下:</strong> <br /><strong>person</strong> <br /><pre name="code" class="java">package mypack; public class person implements java.io.Serializable{ private int id; private String name; private Card mycard; public person(){ } private void setId(int id){ this.id=id; } public void setName(String name){ this.name=name; } public void setMycard(Card mycard){ this.mycard=mycard; } public int getId(){ return this.id; } public String getName(){ return this.name; } public Card getMycard(){ return this.mycard; } } </pre> <br /> <br /><strong>Card</strong> <br /><pre name="code" class="java">package mypack; public class Card { private int id; private String name; public Card(){ } public void setId(int id){ this.id=id; } public void setName(String name){ this.name=name; } public String getName(){ return this.name; } public int getId(){ return this.id; } } </pre> <br /> <br /><br /><strong>问题补充</strong><br />补充下:我是参照网友的教程来做的,地址是:http://ryanpoy.iteye.com/blog/189783 <br />奇怪的是他的结果是正确的!程序里面唯一不同的是我主键的设置上,我用的是auto_increment,映射xml中用的是identity! <br /> <br /><br /><strong>问题补充:</strong><br />谢谢kulinglei的回答,问题是我将cascade="save-update"; 它也没报任何异常啊,正常的情况应该是会报你所说的异常啊 !我把源码上传下,能帮我看看吗?<br /><strong>问题补充:</strong><br />这是源码,希望大家帮帮忙!<br /><strong>问题补充:</strong><br />谢谢etank帮忙,但我也用的是hibernate3的包,并且把identity改为了native。把cascade设为none,结果还是没报任何异常啊?我用的是mysql的数据库
Spring + Hibernate4 无法将数据写入数据【请教】
<div class="iteye-blog-content-contain" style="font-size: 14px;"> <p class="MsoNormal" style="text-align: center;" align="center"> </p> <p class="MsoNormal"><span lang="EN-US"> 问题:</span></p> <p class="MsoNormal"><span lang="EN-US">通过Junit单元测试Service可以将数据写入数据库;但部署却无法向数据库写入数据。</span></p> <h1> <strong><span lang="EN-US">1、</span></strong><span lang="EN-US"><strong>环境</strong>:</span> </h1> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><span lang="EN-US">Spring 3.1.2</span></p> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><span lang="EN-US">Hibernate 4.1.4</span></p> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><span lang="EN-US">Jdk1.6</span></p> <h1>2、配置:</h1> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">1<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">2<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <h2> <span lang="EN-US">2.1、</span><span lang="EN-US">Web.xml</span> </h2> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;?</span><span lang="EN-US">xml</span><span lang="EN-US"> <span lang="EN-US">version</span>=<em><span lang="EN-US">"1.0"</span></em> <span lang="EN-US">encoding</span>=<em><span lang="EN-US">"UTF-8"</span></em><span lang="EN-US">?&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;</span><span lang="EN-US">web-app</span><span lang="EN-US"> <span lang="EN-US">xmlns:xsi</span>=<em><span lang="EN-US">"http://www.w3.org/2001/XMLSchema-instance"</span></em> <span lang="EN-US">xmlns</span>=<em><span lang="EN-US">"http://java.sun.com/xml/ns/javaee"</span></em> <span lang="EN-US">xmlns:web</span>=<em><span lang="EN-US">"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span></em> <span lang="EN-US">xsi:schemaLocation</span>=<em><span lang="EN-US">"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span></em> <span lang="EN-US">id</span>=<em><span lang="EN-US">"WebApp_ID"</span></em> <span lang="EN-US">version</span>=<em><span lang="EN-US">"2.5"</span></em><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;</span><span lang="EN-US">display-name</span><span lang="EN-US">&gt;</span><span style="text-decoration: underline;"><span lang="EN-US">springmvc</span></span><span lang="EN-US">&lt;/</span><span lang="EN-US">display-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;</span><span lang="EN-US">welcome-file-list</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span><span lang="EN-US">index.html</span><span lang="EN-US">&lt;/</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span><span lang="EN-US">index.htm</span><span lang="EN-US">&lt;/</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span><span lang="EN-US">index.jsp</span><span lang="EN-US">&lt;/</span><span lang="EN-US">welcome-file</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;/</span><span lang="EN-US">welcome-file-list</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;!-- Spring ApplicationContext</span><span lang="EN-US">配置文件的路径</span><span lang="EN-US">,</span><span lang="EN-US">可使用通配符</span><span lang="EN-US">,</span><span lang="EN-US">多个路径用</span><span lang="EN-US">,</span><span lang="EN-US">号分隔</span><span lang="EN-US">此参数用于后面的</span><span lang="EN-US">Spring Context Loader --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;</span><span lang="EN-US">context-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">contextConfigLocation</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span style="text-decoration: underline;"><span lang="EN-US">classpath</span></span><span lang="EN-US">*:/applicationContext.xml</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span><span lang="EN-US">&lt;/</span><span lang="EN-US">context-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">listener</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">listener-class</span><span lang="EN-US">&gt;</span><span lang="EN-US">org.springframework.web.context.ContextLoaderListener</span><span lang="EN-US">&lt;/</span><span lang="EN-US">listener-class</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">listener</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!--<span style="text-decoration: underline;">Dispathcer</span> <span style="text-decoration: underline;">Servlet</span> --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">spring-<span style="text-decoration: underline;">mvc</span></span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-class</span><span lang="EN-US">&gt;</span><span lang="EN-US">org.springframework.web.servlet.DispatcherServlet</span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-class</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">contextConfigLocation</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span lang="EN-US">/WEB-INF/spring-mvc.xml</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;/</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">load-on-startup</span><span lang="EN-US">&gt;</span><span lang="EN-US">1</span><span lang="EN-US">&lt;/</span><span lang="EN-US">load-on-startup</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Spring MVC <span style="text-decoration: underline;">Servlet</span> </span><span lang="EN-US">拦截</span><span lang="EN-US">.do</span><span lang="EN-US">结尾的请求</span><span lang="EN-US">--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">spring-<span style="text-decoration: underline;">mvc</span></span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span><span lang="EN-US">*.do</span><span lang="EN-US">&lt;/</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">servlet-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Filter </span><span lang="EN-US">定义</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">encodingFilter</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-class</span><span lang="EN-US">&gt;</span><span lang="EN-US">org.springframework.web.filter.CharacterEncodingFilter</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-class</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">encoding</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span lang="EN-US">UTF-8</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;/</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">forceEncoding</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">            </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span lang="EN-US">true</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;/</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">encodingFilter</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span><span lang="EN-US">&lt;</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span><span lang="EN-US">/*</span><span lang="EN-US">&lt;/</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">   </span><span lang="EN-US">&lt;!--openSessionInView--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">   </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">openSessionInView</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-class</span><span lang="EN-US">&gt;</span><span lang="EN-US">org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-class</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">sessionFactoryBeanName</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><span lang="EN-US">&lt;</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span><span lang="EN-US">sessionFactory</span><span lang="EN-US">&lt;/</span><span lang="EN-US">param-value</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;/</span><span lang="EN-US">init-param</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span><span lang="EN-US">openSessionInView</span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-name</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">&lt;</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span><span lang="EN-US">/*</span><span lang="EN-US">&lt;/</span><span lang="EN-US">url-pattern</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;/</span><span lang="EN-US">filter-mapping</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><span lang="EN-US">&lt;/</span><span lang="EN-US">web-app</span><span lang="EN-US">&gt;</span></p> <h2> <span lang="EN-US">2.2<span lang="EN-US">     </span></span><span lang="EN-US">applicationContext.xml</span> </h2> <p class="MsoNormal"><span lang="EN-US">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;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" xmlns:util="http://www.springframework.org/schema/util" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">          &lt;!--</span><span lang="EN-US">扫描并自动装配</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:annotation-config /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:component-scan base-package="com.sp" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">   &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="locations"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;value&gt;classpath:application.properties&lt;/value&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;/property&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">   &lt;!-- </span><span lang="EN-US">数据源配置</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">    &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;!-- Connection Info --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="driverClassName" value="${jdbc.driver}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="url" value="${jdbc.url}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="username" value="${jdbc.username}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="password" value="${jdbc.password}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;!-- Connection Pooling Info --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="maxActive" value="${dbcp.maxActive}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="maxIdle" value="${dbcp.maxIdle}" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="defaultAutoCommit" value="false" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        </span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;!-- </span><span lang="EN-US">连接</span><span lang="EN-US">Idle</span><span lang="EN-US">一个小时后超时</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="timeBetweenEvictionRunsMillis" value="3600000" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">        &lt;property name="minEvictableIdleTimeMillis" value="3600000" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">    &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;              </span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="hibernateProperties"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;props&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.connection.autocommit"&gt;true&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.current_session_context_class"&gt;org.springframework.orm.hibernate4.SpringSessionContext&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;prop key="hibernate.format_sql"&gt;true&lt;/prop&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;/props&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;/property&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   </span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="packagesToScan"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;list&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;value&gt;com.sp.dao&lt;/value&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                                     &lt;value&gt;com.sp.entity.dict&lt;/value&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;/list&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;/property&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" abstract="false" lazy-init="default" autowire="default"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="sessionFactory"&gt;&lt;ref bean="sessionFactory" /&gt;&lt;/property&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">事务管理配置</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!--&lt;tx:annotation-driven transaction-manager="transactionManager"&gt;&lt;/tx:annotation-driven&gt;--&gt; </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;tx:attributes&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;tx:method name="insert*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                            &lt;tx:method name="get*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">            &lt;tx:method name="add*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">            &lt;tx:method name="update*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">            &lt;tx:method name="delete*" read-only="false" propagation="REQUIRED" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;/tx:attributes&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/tx:advice&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;aop:config&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;aop:advisor pointcut="execution(* com.sp.service.*.*(..))"  advice-ref="txAdvice"/&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/aop:config&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;/beans&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <h2> <span lang="EN-US">2.3<span lang="EN-US"> </span></span><span lang="EN-US">spring-mvc.xml</span> </h2> <p class="MsoNormal"><span lang="EN-US">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span></p> <p class="MsoNormal"><span lang="EN-US">         xmlns:context="http://www.springframework.org/schema/context"</span></p> <p class="MsoNormal"><span lang="EN-US">         xmlns:mvc="http://www.springframework.org/schema/mvc"</span></p> <p class="MsoNormal"><span lang="EN-US">         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd</span></p> <p class="MsoNormal"><span lang="EN-US">                   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd</span></p> <p class="MsoNormal"><span lang="EN-US">        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">启动注解驱动的</span><span lang="EN-US">SpringMVC</span><span lang="EN-US">功能,注册请求</span><span lang="EN-US">URL</span><span lang="EN-US">和注解</span><span lang="EN-US">POJO</span><span lang="EN-US">类方法的映射</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;mvc:annotation-driven /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">自动扫描且只扫描</span><span lang="EN-US">@Controller --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:component-scan base-package="com.sp.web" use-default-filters="false"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/context:component-scan&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:component-scan base-package="com.sp.dao.impl"&gt;&lt;/context:component-scan&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;context:component-scan base-package="com.sp.service.impl"&gt;&lt;/context:component-scan&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;mvc:default-servlet-handler /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">对模型视图名称的解析,在请求时模型视图名称添加后缀。定义</span><span lang="EN-US">JSP --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="prefix" value="/WEB-INF/views/" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">                   &lt;property name="suffix" value=".jsp" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;/bean&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;!-- </span><span lang="EN-US">支持上传文件</span><span lang="EN-US"> --&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">         &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;/beans&gt;</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <h2> <span lang="EN-US">2.4<span lang="EN-US">  </span></span><span lang="EN-US">hibernate.cfg.xml</span> </h2> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;?</span><span lang="EN-US">xml</span><span lang="EN-US"> <span lang="EN-US">version</span>=<em><span lang="EN-US">'1.0'</span></em> <span lang="EN-US">encoding</span>=<em><span lang="EN-US">'utf-8'</span></em><span lang="EN-US">?&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;!</span><span lang="EN-US">DOCTYPE</span><span lang="EN-US"> <span lang="EN-US">hibernate-configuration</span> <span lang="EN-US">PUBLIC</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     <span lang="EN-US">"-//Hibernate/Hibernate Configuration DTD 3.0//EN"</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     <span lang="EN-US">"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">        </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">&lt;</span><span lang="EN-US">hibernate-configuration</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">  </span><span lang="EN-US">&lt;</span><span lang="EN-US">session-factory</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Database connection settings--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.driver_class"</span></em><span lang="EN-US">&gt;</span>com.mysql.jdbc.Driver<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.url"</span></em><span lang="EN-US">&gt;</span>jdbc:mysql://<span style="text-decoration: underline;">localhost</span>:3306/test<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.username"</span></em><span lang="EN-US">&gt;</span>root<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.password"</span></em><span lang="EN-US">&gt;</span>password<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- JDBC connection pool (use the built-in) --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"connection.pool_size"</span></em><span lang="EN-US">&gt;</span>1<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- SQL dialect </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Enable Hibernate's automatic session context management --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt; --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Disable the second-level cache  --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- &lt;property name="cache.provider_class"&gt;org.hibernate.cache.internal.NoCacheProvider&lt;/property&gt;--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Echo all executed SQL to <span style="text-decoration: underline;">stdout</span> --&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"show_sql"</span></em><span lang="EN-US">&gt;</span>true<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"format_sql"</span></em><span lang="EN-US">&gt;</span>true<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;!-- Drop and re-create the database schema on startup--&gt;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"hbm2ddl.auto"</span></em><span lang="EN-US">&gt;</span>update<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">&lt;</span><span lang="EN-US">property</span><span lang="EN-US"> <span lang="EN-US">name</span>=<em><span lang="EN-US">"javax.persistence.validation.mode"</span></em><span lang="EN-US">&gt;</span>none<span lang="EN-US">&lt;/</span><span lang="EN-US">property</span><span lang="EN-US">&gt;</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">     </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">  </span><span lang="EN-US">&lt;/</span><span lang="EN-US">session-factory</span><span lang="EN-US">&gt;</span></p> <p class="MsoNormal"><span lang="EN-US">&lt;/</span><span lang="EN-US">hibernate-configuration</span><span lang="EN-US">&gt;</span></p> <p class="MsoListParagraph" style="margin-left: 18.0pt; text-indent: 0cm;"><strong><span lang="EN-US"> </span></strong></p> <h1> <span lang="EN-US">3<span lang="EN-US">         </span></span><span lang="EN-US">代码:</span> </h1> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">1<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">2<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <p class="MsoListParagraph">&lt;!--[if !supportLists]--&gt;<strong><span lang="EN-US">3<span lang="EN-US">     </span></span></strong>&lt;!--[endif]--&gt;<strong><span lang="EN-US"> </span></strong></p> <h2> <span lang="EN-US">3.1<span lang="EN-US">  </span></span><span lang="EN-US">Service</span><span lang="EN-US">层:</span> </h2> <p class="MsoNormal" align="left"><span lang="EN-US">@Component</span><span lang="EN-US">(</span><span lang="EN-US">"userService"</span><span lang="EN-US">)</span></p> <p class="MsoNormal" align="left"><strong><span lang="EN-US">public</span></strong><strong><span lang="EN-US">class</span></strong><span lang="EN-US"> UserServiceImpl </span><strong><span lang="EN-US">implements</span></strong><span lang="EN-US"> UserService{</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@<span lang="EN-US">Autowired</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">private</span></strong><span lang="EN-US"> UserDao </span><span lang="EN-US">userDao</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@<span lang="EN-US">Autowired</span></span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">private</span></strong><span lang="EN-US"> FormValidator </span><span lang="EN-US">validator</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@Override</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> String userLogin(UserModel user) {</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       String password = </span><span lang="EN-US">userDao</span><span lang="EN-US">.userLogin(user);</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">if</span></strong><span lang="EN-US">(password.equals(user.getPassword())){</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"sucess"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       }</span><strong><span lang="EN-US">else</span></strong><span lang="EN-US">{</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"false"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@Override</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> List&lt;ProjectType&gt; getAllProjectTypes(){</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       List&lt;ProjectType&gt; projectType = </span><span lang="EN-US">userDao</span><span lang="EN-US">.getAllProjectTypes();</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US"> projectType;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">//</span><span lang="EN-US">添加《项目类型》</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@Override</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> String addProjectType(</span><span lang="EN-US">@ModelAttribute</span><span lang="EN-US">(</span><span lang="EN-US">"newProjectType"</span><span lang="EN-US">)ProjectType projectType,BindingResult result,SessionStatus status){</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">validator</span><span lang="EN-US">.validate(projectType, result);</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">if</span></strong><span lang="EN-US">(result.hasErrors()){</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">           </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"newProjectType"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">userDao</span><span lang="EN-US">.save(projectType);</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">//status.setComplete();</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"redirect:showProjectTypes.do"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    }</span></p> <p class="MsoNormal" align="left"><span lang="EN-US"> </span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">//</span><span lang="EN-US">添加《项目类型》</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><span lang="EN-US">@Override</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    </span><strong><span lang="EN-US">public</span></strong><span lang="EN-US"> String addProjectType(ProjectType projectType) {</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><span lang="EN-US">userDao</span><span lang="EN-US">.save(projectType);</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">       </span><strong><span lang="EN-US">return</span></strong><span lang="EN-US">"redirect:showProjectTypes.do"</span><span lang="EN-US">;</span></p> <p class="MsoNormal" align="left"><span lang="EN-US">    }</span></p> <p class="MsoNormal"><span lang="EN-US">}</span></p> <h2> <span lang="EN-US">3.2<span lang="EN-US">   </span></span><span lang="EN-US">Control</span><span lang="EN-US">层</span> </h2> <p class="MsoNormal"><span lang="EN-US">@Controller</span></p> <p class="MsoNormal"><span lang="EN-US">@RequestMapping(value = "/user")</span></p> <p class="MsoNormal"><span lang="EN-US">public class UserController {</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         @Autowired</span></p> <p class="MsoNormal"><span lang="EN-US">         private UserService userService;</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">    @Autowired</span></p> <p class="MsoNormal"><span lang="EN-US">    private FormValidator validator;</span></p> <p class="MsoNormal"><span lang="EN-US">    </span></p> <p class="MsoNormal"><span lang="EN-US">         @RequestMapping(value = "/login",method=RequestMethod.POST)</span></p> <p class="MsoNormal"><span lang="EN-US">         protected String handle(UserModel user,BindingResult result, Model model){</span></p> <p class="MsoNormal"><span lang="EN-US">                   String flag = userService.userLogin(user);</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println("flag:" + flag);</span></p> <p class="MsoNormal"><span lang="EN-US">                   if ("sucess".equals(flag)){</span></p> <p class="MsoNormal"><span lang="EN-US">                            return "sucess";</span></p> <p class="MsoNormal"><span lang="EN-US">                   }</span></p> <p class="MsoNormal"><span lang="EN-US">                   return "test1";</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         @RequestMapping(value = "/showProjectTypes")</span></p> <p class="MsoNormal"><span lang="EN-US">         protected ModelAndView getAllProjectTypes(){</span></p> <p class="MsoNormal"><span lang="EN-US">                   ModelAndView mav = new ModelAndView("showProjectTypes");</span></p> <p class="MsoNormal"><span lang="EN-US">                   List&lt;ProjectType&gt; projectTypes = userService.getAllProjectTypes();</span></p> <p class="MsoNormal"><span lang="EN-US">                   mav.addObject("SEARCH_PROJECTTYPE_RESULTS_KEY", projectTypes);</span></p> <p class="MsoNormal"><span lang="EN-US">                   return mav;</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         @RequestMapping(value = "/addProjectType",method=RequestMethod.GET)</span></p> <p class="MsoNormal"><span lang="EN-US">         protected ModelAndView newProjectTypeForm(){</span></p> <p class="MsoNormal"><span lang="EN-US">                   ModelAndView mav = new ModelAndView("newProjectType");</span></p> <p class="MsoNormal"><span lang="EN-US">                   ProjectType projectType = new ProjectType();</span></p> <p class="MsoNormal"><span lang="EN-US">                   mav.getModelMap().put("newProjectType", projectType);</span></p> <p class="MsoNormal"><span lang="EN-US">                   return mav;</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US">         </span></p> <p class="MsoNormal"><span lang="EN-US">         @RequestMapping(value = "/saveProjectType",method=RequestMethod.POST)</span></p> <p class="MsoNormal"><span lang="EN-US">         protected String addProjectType(@ModelAttribute("newProjectType")ProjectType projectType,BindingResult result,SessionStatus status){</span></p> <p class="MsoNormal"><span lang="EN-US">                   //return userService.addProjectType(projectType, result, status);</span></p> <p class="MsoNormal"><span lang="EN-US">                   return userService.addProjectType(projectType);</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US">}</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <h2> <span lang="EN-US">3.3<span lang="EN-US">    </span></span><span lang="EN-US">DAO</span><span lang="EN-US">@Component("userDao")</span> </h2> <p class="MsoNormal"><span lang="EN-US">public class UserDaoImpl implements UserDao{</span></p> <p class="MsoNormal"><span lang="EN-US">         @Autowired</span></p> <p class="MsoNormal"><span lang="EN-US">         private SessionFactory sessionFactory;</span></p> <p class="MsoNormal"><span lang="EN-US">    </span></p> <p class="MsoNormal"><span lang="EN-US">    public ProjectType getById(int id)</span></p> <p class="MsoNormal"><span lang="EN-US">    {</span></p> <p class="MsoNormal"><span lang="EN-US">        return (ProjectType) sessionFactory.getCurrentSession().get(ProjectType.class, id);</span></p> <p class="MsoNormal"><span lang="EN-US">    }</span></p> <p class="MsoNormal"><span lang="EN-US">    </span></p> <p class="MsoNormal"><span lang="EN-US">         @Override</span></p> <p class="MsoNormal"><span lang="EN-US">         public String userLogin(UserModel user) {</span></p> <p class="MsoNormal"><span lang="EN-US">                   return "123456";</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         //</span><span lang="EN-US">获取数据</span></p> <p class="MsoNormal"><span lang="EN-US">         @Override</span></p> <p class="MsoNormal"><span lang="EN-US">         @SuppressWarnings("unchecked")</span></p> <p class="MsoNormal"><span lang="EN-US">         public List&lt;ProjectType&gt; getAllProjectTypes() {</span></p> <p class="MsoNormal"><span lang="EN-US">                   Criteria ceriteria = sessionFactory.getCurrentSession().createCriteria(ProjectType.class);</span></p> <p class="MsoNormal"><span lang="EN-US">                   return ceriteria.list();</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span lang="EN-US">         //</span><span lang="EN-US">新增数据;</span></p> <p class="MsoNormal"><span lang="EN-US">         @Override</span></p> <p class="MsoNormal"><span lang="EN-US">//      @Transactional(readOnly = true, propagation = Propagation.REQUIRED)</span></p> <p class="MsoNormal"><span lang="EN-US">         public int save(ProjectType projectType) {</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println("save:---");</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println(projectType.getIndex());</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println(projectType.getProjectType());</span></p> <p class="MsoNormal"><span lang="EN-US">                   System.out.println(projectType.getProjectTypeAbbr());</span></p> <p class="MsoNormal"><span lang="EN-US">                   </span></p> <p class="MsoNormal"><span lang="EN-US">                   return (Integer) sessionFactory.getCurrentSession().save(projectType);</span></p> <p class="MsoNormal"><span lang="EN-US">         }</span></p> <p> </p> <p class="MsoNormal"><span lang="EN-US">}</span></p> </div>
Java学习的正确打开方式
在博主认为,对于入门级学习java的最佳学习方法莫过于视频+博客+书籍+总结,前三者博主将淋漓尽致地挥毫于这篇博客文章中,至于总结在于个人,实际上越到后面你会发现学习的最好方式就是阅读参考官方文档其次就是国内的书籍,博客次之,这又是一个层次了,这里暂时不提后面再谈。博主将为各位入门java保驾护航,各位只管冲鸭!!!上天是公平的,只要不辜负时间,时间自然不会辜负你。 何谓学习?博主所理解的学习,它是一个过程,是一个不断累积、不断沉淀、不断总结、善于传达自己的个人见解以及乐于分享的过程。
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过...
大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...
linux系列之常用运维命令整理笔录
本博客记录工作中需要的linux运维命令,大学时候开始接触linux,会一些基本操作,可是都没有整理起来,加上是做开发,不做运维,有些命令忘记了,所以现在整理成博客,当然vi,文件操作等就不介绍了,慢慢积累一些其它拓展的命令,博客不定时更新 free -m 其中:m表示兆,也可以用g,注意都要小写 Men:表示物理内存统计 total:表示物理内存总数(total=used+free) use...
比特币原理详解
一、什么是比特币 比特币是一种电子货币,是一种基于密码学的货币,在2008年11月1日由中本聪发表比特币白皮书,文中提出了一种去中心化的电子记账系统,我们平时的电子现金是银行来记账,因为银行的背后是国家信用。去中心化电子记账系统是参与者共同记账。比特币可以防止主权危机、信用风险。其好处不多做赘述,这一层面介绍的文章很多,本文主要从更深层的技术原理角度进行介绍。 二、问题引入 假设现有4个人...
程序员接私活怎样防止做完了不给钱?
首先跟大家说明一点,我们做 IT 类的外包开发,是非标品开发,所以很有可能在开发过程中会有这样那样的需求修改,而这种需求修改很容易造成扯皮,进而影响到费用支付,甚至出现做完了项目收不到钱的情况。 那么,怎么保证自己的薪酬安全呢? 我们在开工前,一定要做好一些证据方面的准备(也就是“讨薪”的理论依据),这其中最重要的就是需求文档和验收标准。一定要让需求方提供这两个文档资料作为开发的基础。之后开发...
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))
今天闲着无事,就想写点东西。然后听了下歌,就打算写个播放器。 于是乎用h5 audio的加上js简单的播放器完工了。 演示地点演示 html代码如下` music 这个年纪 七月的风 音乐 ` 然后就是css`*{ margin: 0; padding: 0; text-decoration: none; list-...
Python十大装B语法
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并附上详细的实例代码。如能在实战中融会贯通、灵活使用,必将使代码更为精炼、高效,同时也会极大提升代码B格,使之看上去更老练,读起来更优雅。
数据库优化 - SQL优化
以实际SQL入手,带你一步一步走上SQL优化之路!
2019年11月中国大陆编程语言排行榜
2019年11月2日,我统计了某招聘网站,获得有效程序员招聘数据9万条。针对招聘信息,提取编程语言关键字,并统计如下: 编程语言比例 rank pl_ percentage 1 java 33.62% 2 cpp 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7 p...
通俗易懂地给女朋友讲:线程池的内部原理
餐盘在灯光的照耀下格外晶莹洁白,女朋友拿起红酒杯轻轻地抿了一小口,对我说:“经常听你说线程池,到底线程池到底是个什么原理?”
《奇巧淫技》系列-python!!每天早上八点自动发送天气预报邮件到QQ邮箱
将代码部署服务器,每日早上定时获取到天气数据,并发送到邮箱。 也可以说是一个小型人工智障。 知识可以运用在不同地方,不一定非是天气预报。
经典算法(5)杨辉三角
杨辉三角 是经典算法,这篇博客对它的算法思想进行了讲解,并有完整的代码实现。
英特尔不为人知的 B 面
从 PC 时代至今,众人只知在 CPU、GPU、XPU、制程、工艺等战场中,英特尔在与同行硬件芯片制造商们的竞争中杀出重围,且在不断的成长进化中,成为全球知名的半导体公司。殊不知,在「刚硬」的背后,英特尔「柔性」的软件早已经做到了全方位的支持与支撑,并持续发挥独特的生态价值,推动产业合作共赢。 而对于这一不知人知的 B 面,很多人将其称之为英特尔隐形的翅膀,虽低调,但是影响力却不容小觑。 那么,在...
腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹?
昨天,有网友私信我,说去阿里面试,彻底的被打击到了。问了为什么网上大量使用ThreadLocal的源码都会加上private static?他被难住了,因为他从来都没有考虑过这个问题。无独有偶,今天笔者又发现有网友吐槽了一道腾讯的面试题,我们一起来看看。 腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹? 在互联网职场论坛,一名程序员发帖求助到。二面腾讯,其中一个算法题:64匹...
面试官:你连RESTful都不知道我怎么敢要你?
干货,2019 RESTful最贱实践
刷了几千道算法题,这些我私藏的刷题网站都在这里了!
遥想当年,机缘巧合入了 ACM 的坑,周边巨擘林立,从此过上了"天天被虐似死狗"的生活… 然而我是谁,我可是死狗中的战斗鸡,智力不够那刷题来凑,开始了夜以继日哼哧哼哧刷题的日子,从此"读题与提交齐飞, AC 与 WA 一色 ",我惊喜的发现被题虐既刺激又有快感,那一刻我泪流满面。这么好的事儿作为一个正直的人绝不能自己独享,经过激烈的颅内斗争,我决定把我私藏的十几个 T 的,阿不,十几个刷题网...
为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?
关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。 声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。 一、事件起因 关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看...
白话阿里巴巴Java开发手册高级篇
不久前,阿里巴巴发布了《阿里巴巴Java开发手册》,总结了阿里巴巴内部实际项目开发过程中开发人员应该遵守的研发流程规范,这些流程规范在一定程度上能够保证最终的项目交付质量,通过在时间中总结模式,并推广给广大开发人员,来避免研发人员在实践中容易犯的错误,确保最终在大规模协作的项目中达成既定目标。 无独有偶,笔者去年在公司里负责升级和制定研发流程、设计模板、设计标准、代码标准等规范,并在实际工作中进行...
SQL-小白最佳入门sql查询一
不要偷偷的查询我的个人资料,即使你再喜欢我,也不要这样,真的不好;
项目中的if else太多了,该怎么重构?
介绍 最近跟着公司的大佬开发了一款IM系统,类似QQ和微信哈,就是聊天软件。我们有一部分业务逻辑是这样的 if (msgType = "文本") { // dosomething } else if(msgType = "图片") { // doshomething } else if(msgType = "视频") { // doshomething } else { // doshom...
Nginx 原理和架构
Nginx 是一个免费的,开源的,高性能的 HTTP 服务器和反向代理,以及 IMAP / POP3 代理服务器。Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻名。 Nginx 的整体架构 Nginx 里有一个 master 进程和多个 worker 进程。master 进程并不处理网络请求,主要负责调度工作进程:加载配置、启动工作进程及非停升级。worker 进程负责处...
【图解经典算法题】如何用一行代码解决约瑟夫环问题
约瑟夫环问题算是很经典的题了,估计大家都听说过,然后我就在一次笔试中遇到了,下面我就用 3 种方法来详细讲解一下这道题,最后一种方法学了之后保证让你可以让你装逼。 问题描述:编号为 1-N 的 N 个士兵围坐在一起形成一个圆圈,从编号为 1 的士兵开始依次报数(1,2,3…这样依次报),数到 m 的 士兵会被杀死出列,之后的士兵再从 1 开始报数。直到最后剩下一士兵,求这个士兵的编号。 1、方...
吐血推荐珍藏的Visual Studio Code插件
作为一名Java工程师,由于工作需要,最近一个月一直在写NodeJS,这种经历可以说是一部辛酸史了。好在有神器Visual Studio Code陪伴,让我的这段经历没有更加困难。眼看这段经历要告一段落了,今天就来给大家分享一下我常用的一些VSC的插件。 VSC的插件安装方法很简单,只需要点击左侧最下方的插件栏选项,然后就可以搜索你想要的插件了。 下面我们进入正题 Material Theme ...
如何防止抄袭PCB电路板
目录 1、抄板是什么 2、抄板是否属于侵权 3、如何防止抄板 1、抄板是什么 抄板也叫克隆或仿制,是对设计出来的PCB板进行反向技术研究;目前全新的定义:从狭义上来说,抄板仅指对电子产品电路板PCB文件的提取还原和利用文件进行电路板克隆的过程;从广义上来说,抄板不仅包括对电路板文件提取、电路板克隆、电路板仿制等技术过程,而且包括对电路板文件进行修改(即改板)、对电子产品外形模具进行三维...
“狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作
一、垃圾文字生成器介绍 最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。 项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator 根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。 他的文风可能是这样的: 你发现,...
程序员:我终于知道post和get的区别
是一个老生常谈的话题,然而随着不断的学习,对于以前的认识有很多误区,所以还是需要不断地总结的,学而时习之,不亦说乎
《程序人生》系列-这个程序员只用了20行代码就拿了冠军
你知道的越多,你不知道的越多 点赞再看,养成习惯GitHub上已经开源https://github.com/JavaFamily,有一线大厂面试点脑图,欢迎Star和完善 前言 这一期不算《吊打面试官》系列的,所有没前言我直接开始。 絮叨 本来应该是没有这期的,看过我上期的小伙伴应该是知道的嘛,双十一比较忙嘛,要值班又要去帮忙拍摄年会的视频素材,还得搞个程序员一天的Vlog,还要写BU...
加快推动区块链技术和产业创新发展,2019可信区块链峰会在京召开
11月8日,由中国信息通信研究院、中国通信标准化协会、中国互联网协会、可信区块链推进计划联合主办,科技行者协办的2019可信区块链峰会将在北京悠唐皇冠假日酒店开幕。   区块链技术被认为是继蒸汽机、电力、互联网之后,下一代颠覆性的核心技术。如果说蒸汽机释放了人类的生产力,电力解决了人类基本的生活需求,互联网彻底改变了信息传递的方式,区块链作为构造信任的技术有重要的价值。   1...
Python 植物大战僵尸代码实现(2):植物卡片选择和种植
这篇文章要介绍的是: - 上方植物卡片栏的实现。 - 点击植物卡片,鼠标切换为植物图片。 - 鼠标移动时,判断当前在哪个方格中,并显示半透明的植物作为提示。
相关热词 c# clr dll c# 如何orm c# 固定大小的字符数组 c#框架设计 c# 删除数据库 c# 中文文字 图片转 c# 成员属性 接口 c#如何将程序封装 16进制负数转换 c# c#练手项目
立即提问