SSH整合,第二篇。

创建工程

这里只是测试和理解hibernate。建立Java工程就好了。

1、hibernate-4.2.21.jar

hibernate包下的required,即\hibernate-release-4.2.21.Final\required。

2、hibernate.cfg.xml

 <?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> <!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/dbfortest</property>
<property name="connection.username">root</property>
<property name="connection.password">root</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> <!-- format_sql -->
<property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup
validate |create|create-drop|update
-->
<!-- <property name="hbm2ddl.auto">update</property> --> <!-- mapping 注解的是class="....." --->
<mapping class="com.xzw.ssh.pojo.User" />
<mapping class="com.xzw.ssh.pojo.Permission" />
<mapping class="com.xzw.ssh.pojo.UserAndRole" />
<mapping class="com.xzw.ssh.pojo.Role" />
<mapping class="com.xzw.ssh.pojo.RoleAndPermission" /> </session-factory> </hibernate-configuration>

hibernate.cfg.xml

注意在classpath下的,不然注意configure设置路径。上面只打开部分要用到属性

3、编写hbm.xml

创建表后,可以用逆向工程来生成annotation的实体类和*.hbm.xml。

4、创建数据库表

可以在创建好*.hbm.xml后,通过<property name="hbm2ddl.auto">create</property>来建表。

5、 最后

由于hibernate的正向和逆向工程的存在,建表或建xml可以自动生成,可以分别生成,然后对比一下,如果不符合要求的列,或属性,可以手动修改。半自动化操作………… -_-

本次的表和关系可以参考上篇

         @Test
public void test1() {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession(); session.beginTransaction();
User user = (User) session.get(User.class,"40ec81e856a45e190156a45e1b600000");
System.out.println(user.getGender());
Set<UserAndRole> userAndRoles =user.getUserAndRoles();
Iterator it = userAndRoles.iterator();
while(it.hasNext()){
UserAndRole uar = (UserAndRole) it.next();
Role role = uar.getRole();
System.out.println(role.getName());
}
session.getTransaction().commit();
}
@Test
public void test2(){
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
String hql = "from User user where user.username='reader1'";
Query q = session.createQuery(hql);
System.out.println("Query");
List<User> users = q.list();
for(User u : users){
String psd = u.getPassword();
System.out.println(psd);
}
session.close();
sessionFactory.close();
}

测试方法

SSH整合 第二篇 工程初建的更多相关文章

  1. 第二篇 Python初识别及变量名定义规范

    第一个Python程序 可以打开notepad或者其他文本编辑器,输入:print("Hello Python!"),将文件保存到任意盘符下,后缀名是  .py 两种python程 ...

  2. 初建FreeMarker工程

    初建FreeMarker工程 ——@梁WP 背景:听说freemarker可以用来写页面的组件,热衷于编写可重用代码的我,迫不及待地研究了freemarker,不过,在写组件之前,还是先研究一下fre ...

  3. Spring cloud系列教程第二篇:支付项目父工程图文搭建

    Spring cloud系列教程第二篇:支付项目父工程图文搭建 在讲解spring cloud相关的技术的时候,咱们就模拟订单支付这个流程来讲讲 在这个支付模块微服务搭建过程中,上面的这些技术,都会融 ...

  4. SpringCloud实战 | 第二篇:SpringCloud整合Nacos实现注册中心

    前言 随着eureka的停止更新,如果同时实现注册中心和配置中心需要SpringCloud Eureka和SpringCloud Config两个组件;配置修改刷新时需要SpringCloud Bus ...

  5. 从MVC和三层架构说到SSH整合开发

    相信很多人都认同JavaWeb开发是遵从MVC开发模式的,遵从三层架构进行开发的,是的,大家都这么认同.但是相信大家都会有过这样一个疑问,if(MVC三层模式==三层架构思想)out.println( ...

  6. SSH整合方案2

    [案例3]SSH整合_方案2 **  案例描述  两个知识点的演示  其一,SSH整合的第二个方案  其二,Spring+JDBC+Struts2  参考代码  31) 使用工程spring4  32 ...

  7. ASP.NET自定义控件组件开发 第一章 第二篇 接着待续

    原文:ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 很感谢大家给我的第一篇ASP.NET控件开发的支持!在写这些之前,我也看了 ...

  8. Spring+SpringMVC+MyBatis+easyUI整合优化篇(十三)数据层优化-表规范、索引优化

    本文提要 最近写的几篇文章都是关于数据层优化方面的,这几天也在想还有哪些地方可以优化改进,结合日志和项目代码发现,关于数据层的优化,还是有几个方面可以继续修改的,代码方面,整合了druid数据源也开启 ...

  9. SSH整合:Unable to instantiate Action, employeeAction, defined for 'emp-list' in namespace '/'employeeAction - action

    SSH整合,照着视频敲的,不知为何会报错,经历了快两周的折磨给解决了.记录下来给后面需要帮助的人,也算极好的了. Struts Problem Report Struts has detected a ...

随机推荐

  1. (转载)session token机制

    http://blog.chinaunix.net/uid-26642709-id-3061264.html 使用session token时,必须用struts2表标签库,不能用html 通过ses ...

  2. 2017.1.9版给信息源新增:max_len、max_db字段

    2017.1.8a版程序给信息源增加max_len.max_db字段,分别用于控制:获取条数.数据库保留条数. max_len的说明见此图: max_db的说明见此图: 当max_len和max_db ...

  3. win10下关于apache配置虚拟主机

    apache安装完默认是不开启虚拟服务器的,如果希望在本地apache上面配置虚拟服务器,类似于在网上买的虚拟主机,可以按照以下步骤进行配置: 1,修改本机的hosts文件,如下 示例:127.0.0 ...

  4. 大型运输行业实战_day09_2_站间互售实现

    1.添加站间互售入口 对应的html代码 <button onclick="otherStation()">站间互售</button> 对应的js发送函数 ...

  5. 84. Largest Rectangle in Histogram (Array, Stack; DP)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. MyBatis核心配置文件详解

    ------------------------siwuxie095                                     MyBatis 核心配置文件详解         1.核心 ...

  7. Intellij IDEA 热部署处理

     1. 首先参考IDEA热部署同行经验分享:  Intellij IDEA 4种配置热部署的方法 2. IDEA 热部署实战: springboot项目: 不要引入热部署工具包spring-boot- ...

  8. [leetcode]632. Smallest Range最小范围

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  9. WebBrowser-Javascript与C++互操作

    WebBrowser控件是Microsoft提供的一个用于网页浏览的客户端控件,WebBrowser控件的使用相当广泛,例如很多邮件客户端都是使用可编辑的WebBrowser控件作为写邮件的工具,也有 ...

  10. Boost 库uuid 的使用

    UUID 简介 通用唯一识别码(英语:Universally Unique Identifier,简称UUID)是一种软件建构的标准,亦为开放软件基金会组织在分布式计算环境领域的一部分. uuid 版 ...