最近在学习ssh把一些小demo记录下来以备日后查看
各位兄弟有什么好的建议,那就请不要吝啬,请多多指点。
以上是两个pojo类由于是学习用的就弄的简单些把密码和用户名给分开,再次声明这只是测试用真正的开发中这绝对是一bad smell
下面是对应的配置文件
下面就是hibernate 的配置文件拉
下面是测试用类
OK现在可以去数据库里看结果拉
各位兄弟有什么好的建议,那就请不要吝啬,请多多指点。
java 代码
- package com.tyq.pojo;
- public class User implements java.io.Serializable {
- // Fields
- private int id;
- private String name;
- private Password password = null;
- public Password getPassword() {
- return password;
- }
- public void setPassword(Password password) {
- this.password = password;
- }
- /** default constructor */
- public User() {
- }
- // Property accessors
- public int getId() {
- return this.id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
java 代码
- package com.tyq.pojo;
- public class Password implements java.io.Serializable {
- // Fields
- private int id;
- private String password;
- private User user = null;
- // Constructors
- /** default constructor */
- public Password() {
- }
- // Property accessors
- public int getId() {
- return this.id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getPassword() {
- return this.password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- }
下面是对应的配置文件
xml 代码
- <!--User.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">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.tyq.pojo.User" table="user">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="identity" />
- </id>
- <property name="name" type="java.lang.String">
- <column name="name" length="20" unique="true" />
- </property>
- <one-to-one name="password"
- class="com.tyq.pojo.Password"
- cascade="all"/>
- </class>
- </hibernate-mapping>
xml 代码
- <1--Password.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">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.tyq.pojo.Password" table="password">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="identity"></generator>
- </id>
- <property name="password" type="java.lang.String">
- <column name="password" length="40" not-null="true" unique="true" />
- </property>
- <one-to-one name="user"
- class="com.tyq.pojo.User"
- constrained="true"/>
- </class>
- </hibernate-mapping>
xml 代码
- <!--hibernate.cfg.xml-->
- <?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">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <property name="myeclipse.connection.profile">
- com.jdbc.mysql.Driver
- </property>
- <property name="connection.url">
- jdbc:mysql://localhost:3306/system
- </property>
- <property name="connection.username">yourname</property>
- <property name="connection.password">yourpassword</property>
- <property name="connection.driver_class">
- com.mysql.jdbc.Driver
- </property>
- <property name="dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <property name="show_sql">true</property>
- <mapping resource="com/tyq/pojo/User.hbm.xml" />
- <mapping resource="com/tyq/pojo/Password.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
java 代码
- import junit.framework.TestCase;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import com.tyq.pojo.Password;
- import com.tyq.pojo.User;
- public class TestHibernate extends TestCase {
- Session session = null;
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- Configuration config = new Configuration().configure();
- SessionFactory factory = config.buildSessionFactory();
- session = factory.openSession();
- }
- @Override
- protected void tearDown() throws Exception {
- if(session != null)
- {
- session.close();
- }
- super.tearDown();
- }
- public void testSelect(){
- User user = new User();
- Password password = new Password();
- user.setName("lql");
- password.setPassword("tyq");
- user.setPassword(password);
- password.setUser(user);
- session.save(user);
- session.beginTransaction().commit();
- }
- }