inotgo.com

Download area
File name File size
spring_hibernate.rar 12m

Tool version compatibility problem
The integration idea is , send DAO Inherit HibernateTemplate This class
HibernateTemplate This class provides setSessionFactory() Method for injection SessionFactory
Through spring obtain DAO When I was , injection SessionFactory.
Correct

Step 1 : pojo   
Step 2 : hbm   
Step 3 : DAO   
Step 4 : applicationContext.xml   
Step 5 : Test query   
Step 6 : Test increase Delete change   

package com.how2java.pojo; public class Category { 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; } private int id; private String name; }
package com.how2java.pojo;
 
public class Category {
 
    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;
    }
    private int id;
    private String name;
}
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.how2java.pojo"> <class name="Category" table="category_"> <id name="id" column="id"> <generator class="native"> </generator> </id> <property name="name" /> </class> </hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.how2java.pojo">
	<class name="Category" table="category_">
		<id name="id" column="id">
			<generator class="native">
			</generator>
		</id>
		<property name="name" />

	</class>
	
</hibernate-mapping>
DAO Inherit HibernateTemplete, and HibernateTemplete There is one in the class setSessionFactory For receiving sessionFactory Injection of
package com.how2java.dao; import org.springframework.orm.hibernate3.HibernateTemplate; public class CategoryDAO extends HibernateTemplate{ }
package com.how2java.dao;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class CategoryDAO extends HibernateTemplate{

}
establish dao When I was , Will inject sessionfactory
establish sessionFactory It will be injected into the data source ds
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="c" class="com.how2java.pojo.Category"> <property name="name" value="yyy" /> </bean> <bean name="dao" class="com.how2java.dao.CategoryDAO"> <property name="sessionFactory" ref="sf" /> </bean> <bean name="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="ds" /> <property name="mappingResources"> <list> <value>com/how2java/pojo/Category.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hbm2ddl.auto=update </value> </property> </bean> <bean name="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="admin" /> </bean> </beans>
Because CategoryDAO Inherited HibernateTemplate, So you can directly use
find To query all Category
 Test query
package com.how2java.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.how2java.dao.CategoryDAO; import com.how2java.pojo.Category; public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); CategoryDAO dao = (CategoryDAO) context.getBean("dao"); List<Category> cs= dao.find("from Category c"); System.out.println(cs); } }
package com.how2java.test;
 
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.how2java.dao.CategoryDAO;
import com.how2java.pojo.Category;
 
public class TestSpring {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        CategoryDAO dao = (CategoryDAO) context.getBean("dao");
        List<Category> cs= dao.find("from Category c");
        System.out.println(cs);
    }
}
Because CategoryDAO Inherited HibernateTemplate, So you can directly use
1. save Add
2. get obtain
3. update modify
4. delete Delete
package com.how2java.test; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.how2java.dao.CategoryDAO; import com.how2java.pojo.Category; public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); CategoryDAO dao = (CategoryDAO) context.getBean("dao"); Category c = new Category(); c.setName("category yyy"); // Add dao.save(c); // obtain Category c2 = dao.get(Category.class, 1); // modify c2.setName("category zzz"); dao.update(c2); // Delete dao.delete(c2); } }


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2020-05-19 Why didn't you create a database and table structure
ssa

How to create databases and tables without statements




3 One answer

Double integral ruined my youth
Answer time :2021-10-13
Build your own pojo The creation of properties in the class is finished and ahead hibernate and spring The beginning of the chapter has the process of creating tables
CREATE TABLE category_ (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(32) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

ForMySunny
Answer time :2020-06-14
You're wrong = = stay Category.hbm.xml It's defined

ForMySunny
Answer time :2020-06-13
Build one yourself



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-02-18 hibernate.hbm2ddl.auto=update
nicococococ




hibernate.hbm2ddl.auto=update hibernate Cannot be omitted
hibernate.hbm2ddl.auto=update  
hibernate.hbm2ddl.auto=update  


3 One answer

Glittering
Answer time :2020-07-11
yes , I found out, too , I've been reading your reminder for a long time. I'll try it

ABW
Answer time :2020-06-03
The previous answer is wrong , This is not the reason for the error report , The error message appears because... Cannot be deleted id=1, The specific reason is unknown .

ABW
Answer time :2020-06-03
Otherwise, it will not be updated automatically , cause delete(c1) Times error : Exception in thread "main" org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [delete from category_ where id=?]; constraint [null];



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





2020-02-18 How tables are automatically generated
2019-02-13 Dialect is really hard to remember
2018-11-03 Why after dependency injection hibernate No beginTransaction Yes


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 16 Previous questions , please Click to view

Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about JAVA Frame -Spring+Hibernate- Integration steps Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot