hibernate:one

版权声明:java洪君 https://blog.csdn.net/qq_43532342/article/details/85091830
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.password">root</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hcsql?characterEncoding=utf-8</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<mapping resource="com/big/entity/User.hbm.xml" />
	</session-factory>
</hibernate-configuration>

hibernate的配置文件,连接池

package com.big.entity;

public class User {

	private Integer userId;
	private String userName;

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public User(String userName) {
		super();
		this.userName = userName;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

}

对象

<?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 2018-8-27 12:03:35 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
	<class name="com.big.entity.User" table="USER">
		<id name="userId" type="java.lang.Integer">
			<column name="USERID" />
			<generator class="native" />
		</id>
		<property name="userName" type="java.lang.String">
			<column name="USERNAME" />
		</property>
	</class>
</hibernate-mapping>

对象配置

package com.big.action;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.big.entity.User;
import com.opensymphony.xwork2.ActionSupport;

public class BigAction extends ActionSupport {
	private List<User> bigUsers;

	public String big() {

		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Transaction transaction = session.beginTransaction();

		bigUsers = session.createQuery("from User").list();

		transaction.commit();
		session.close();
		factory.close();
		return SUCCESS;

	}

	public List<User> getBigUsers() {
		return bigUsers;
	}

	public void setBigUsers(List<User> bigUsers) {
		this.bigUsers = bigUsers;
	}

}

打开资源文件,直接可以增删改查

猜你喜欢

转载自blog.csdn.net/qq_43532342/article/details/85091830
今日推荐