SSH整合 第二篇 工程初建
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整合 第二篇 工程初建的更多相关文章
- 第二篇 Python初识别及变量名定义规范
第一个Python程序 可以打开notepad或者其他文本编辑器,输入:print("Hello Python!"),将文件保存到任意盘符下,后缀名是 .py 两种python程 ...
- 初建FreeMarker工程
初建FreeMarker工程 ——@梁WP 背景:听说freemarker可以用来写页面的组件,热衷于编写可重用代码的我,迫不及待地研究了freemarker,不过,在写组件之前,还是先研究一下fre ...
- Spring cloud系列教程第二篇:支付项目父工程图文搭建
Spring cloud系列教程第二篇:支付项目父工程图文搭建 在讲解spring cloud相关的技术的时候,咱们就模拟订单支付这个流程来讲讲 在这个支付模块微服务搭建过程中,上面的这些技术,都会融 ...
- SpringCloud实战 | 第二篇:SpringCloud整合Nacos实现注册中心
前言 随着eureka的停止更新,如果同时实现注册中心和配置中心需要SpringCloud Eureka和SpringCloud Config两个组件;配置修改刷新时需要SpringCloud Bus ...
- 从MVC和三层架构说到SSH整合开发
相信很多人都认同JavaWeb开发是遵从MVC开发模式的,遵从三层架构进行开发的,是的,大家都这么认同.但是相信大家都会有过这样一个疑问,if(MVC三层模式==三层架构思想)out.println( ...
- SSH整合方案2
[案例3]SSH整合_方案2 ** 案例描述 两个知识点的演示 其一,SSH整合的第二个方案 其二,Spring+JDBC+Struts2 参考代码 31) 使用工程spring4 32 ...
- ASP.NET自定义控件组件开发 第一章 第二篇 接着待续
原文:ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 很感谢大家给我的第一篇ASP.NET控件开发的支持!在写这些之前,我也看了 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(十三)数据层优化-表规范、索引优化
本文提要 最近写的几篇文章都是关于数据层优化方面的,这几天也在想还有哪些地方可以优化改进,结合日志和项目代码发现,关于数据层的优化,还是有几个方面可以继续修改的,代码方面,整合了druid数据源也开启 ...
- SSH整合:Unable to instantiate Action, employeeAction, defined for 'emp-list' in namespace '/'employeeAction - action
SSH整合,照着视频敲的,不知为何会报错,经历了快两周的折磨给解决了.记录下来给后面需要帮助的人,也算极好的了. Struts Problem Report Struts has detected a ...
随机推荐
- Haskell语言学习笔记(34)System.Environment
System.Environment getArgs :: IO [String] getProgName :: IO String getExecutablePath :: IO FilePath ...
- layoutSubviews相关总结
ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit ------- - (void)layoutSubview ...
- js 删除节点,jquery遍历通过内容定位节点
$(".class1 .class2").each(function (index, item) { var gettedValue = $(item).find(".c ...
- SQL查询效率where语句条件
有索引的列优先,都有索引的看查询出来的数据量,少的优先 in ,not in,<>,is null,is not null 等由于不会走索引,尽量不要使用. WHERE子句后面的条件顺序对 ...
- 安装Android模拟器
说明:可以直接通过Android SDK目录下的SDK Manager.exe进行安装,我这里写的是通过下载各种文件进行安装的方法,没有通过SDK Manager.exe进行下载安装 一.下载以下内容 ...
- 格式化java8 LocalDateTime
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(time1);
- 各种replace待续
http://blog.163.com/chenjie_8392/blog/static/439339842010513128139/
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)
1. 步骤一:恢复转账开发环境(转账开发环境见“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步骤二:引入AOP的开发包3.步骤三:引入 ...
- css样式优先级和权重问题
内联样式: <div style="font-size: 12px;">姓名</div> 外部样式: <link rel="styleshe ...