IDEA添加Hibernate框架以及常见问题的解决
IDEA添加Hibernate框架以及常见问题的解决
主要描述:
- 在IDEA项目中添加Hibernate框架的步骤
- SpringMVC+Hibernate会遇到的常见问题
在IDEA项目中添加Hibernate框架
-
添加数据库
-
数据库表结构
-
idea中添加数据源
-
连接数据库
-
-
项目中添加对hibernate框架
-
右键项目名添加hibernate支持
-
选择创建配置文件以及生成实体
-
选择数据源以,需要生成实体的表,生成实体类的名字前后缀以及存放位置
-
自动生成的项目结构
其中Main为自动生成的测试类
此时还还需要几步配置才能使得Main能够正常运行
-
Unable to load class [com.mysql.jdbc.Driver]
导入mysql-connector-jar-8.0.13.jar
-
Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
hibernate.cfg.xml中配置:
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
-
ERROR: Access denied for user ‘’@‘localhost’ (using password: NO)
hibernate.cfg.xml中配置账号密码
<property name="connection.username">root</property> <property name="connection.password">123</property>
-
Main测试时,是在src目录下寻找hibernate.cfg.xml
正常运行输出
executing: from UsersEntity [email protected]
-
-
SpringMVC+Hibernate会遇到的常见问题
分包好之后的项目结构
-
hibernate.cfg.xml文件中com.mysql.jdbc.Driver标红
导入MySQL-connector包即可
-
tomcat启动后 调用hibernate内容 :/hibernate.cfg.xml not found
将hibernate.cfg.xml添加到web/WEB-INF/classes目录下
-
Artifact中需要添加hibernate和MySQL的库
-
使用示例
action:
@Controller public class UserController { @RequestMapping(value = "users",method = RequestMethod.GET) public String listUsers(ModelMap map){ Session session= Dbconnection.getSession(); List<UsersEntity> list =session.createCriteria(UsersEntity.class).list(); map.addAttribute("users",list); session.close(); return "users"; }
users.jsp
<ol> <%for(UsersEntity e:(List<UsersEntity>)request.getAttribute("users")){%> <li>名字:<%out.print(e.getName());%>,年龄:<%out.print(e.getAge());%> <%}%> </ol>