IDEA 下写出第一个 SSH 整合框架练手项目( 一 ,Spring 与 Hibernate 整合)
IDEA 下写出第一个 SSH 整合框架练手项目( 一 )
最近几周快速的学完了 SSH 框架,因为很快,所以几乎没记住什么,又抄着视频写了一个项目,决定自己只看源码的再写一遍。
视频地址: 基于SSH实现员工管理系统之案例实现篇
我的源码地址 : 点击进入 github
1. 新建meavn项目
首先,新建一个Meavn 项目,File <--- New <----- Project <---- Meavn <----- next
然后 grpoupId 随便写写,artifactid 写项目名就行。
新建项目的文件结构图如下
将下面的内容中的 groupId 和artifactId 更改为自己对应的groupId 和artifactId,直接替换到 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>learing</groupId>
<!--你的group ID,进行更改即可-->
<artifactId>SSH_002</artifactId>
<!--你的artifactId,进行更改即可 -->
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.1.1.RELEASE</spring.version>
<hibernate.version>3.6.5.Final</hibernate.version>
<struts2.version>2.3.1</struts2.version>
</properties>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<!--config-browser-plugin插件,使用了这个插件之后,就可以很方便的浏览项目中的所有action及其与 jsp view的映射 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>${struts2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- 添加对数据库的支持 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
</dependency>
<!-- 添加对数据源的支持 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<webResources>
<resource>
<directory>src\main\webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<finalName>com</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.hbm.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
然后右下角弹窗 点击自动导入即可。
2.新建 Java bean (Employee 和 Department)
新建包 com.test.ssh.domain
在domain 包下新建 Employee 和 Department 类
Employee.java 的代码如下(属性,get方法和set方法)
package com.test.ssh.domain;
import java.util.Date;
public class Employee {
private int eid; //员工的编号
private String ename; //名称
private String sex; //性别
private Date birthday; //生日
private Date joinDate; //入职日期
private String eno; //员工的部门的编号
private String username; //用户登录账号
private String password; //用户登陆密码
//所属部门
private Department department;
//getter 和 setter
}
Department.java
package com.test.ssh.domain;
import java.util.HashSet;
import java.util.Set;
public class Department {
private int did; //部门的id
private String dname; //部门名称
private String ddesc; //部门的描述
//部门对应的员工的集合
private Set<Employee> employees = new HashSet<Employee>();
//getter 和 setter
}
因为教学视频中使用的是基于配置文件的模式,所以我也使用配置文件,以防止错误。
Department.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.ssh.domain.Department" table="department">
<id name="did" column="did">
<generator class="native"/>
<!-- 整型ID, 所以使用 native -->
</id>
<!-- 部门名称-->
<property name="dname" column="dname" length="20"/>
<!-- 部门地址 -->
<property name="ddesc" column="ddesc" length="100"/>
<!--一对多映射-->
<set name="employees" cascade="delete" inverse="true">
<!--
cascade ="delete" 意为:在执行 delete操作时进行关联操作
inverse = "true"
-->
<key column="dno"/>
<one-to-many class="com.test.ssh.domain.Employee"/>
</set>
</class>
</hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.ssh.domain.Employee" table="employee">
<id name="eid" column="eid">
<generator class="native"/>
</id>
<property name="ename" column="ename" length="20"/>
<property name="sex" column="sex" length="10"/>
<property name="birthday" column="birthday"/>
<property name="joinDate" column="joindate"/>
<property name="eno" column="eno" length="20"/>
<property name="username" column="username" length="20"/>
<property name="password" column="password" length="20"/>
<!-- column是employee表对应department表的外键名称 -->
<many-to-one name="department" class="com.test.ssh.domain.Department" column="dno" lazy="false"/>
</class>
</hibernate-mapping>
然后在 resources 下新建一个log4j.properties
log4j.rootLogger=DEBUG,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.s=console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.appender.org.apache=INFO
3. Hibernate与 Spring 整合
新建Spring 配置文件 applicationContext.xml,右键 resources ,new ---> XML Configuration File ----->Spring Config
为了便于管理数据库信息。在 resources 文件夹中建立一个外联文件 db.properties 用来保存数据库的重要信息
jdbc.username=root
#数据库账号
jdbc.password=123456
#数据库密码
jdbc.driverClass=com.mysql.jdbc.Driver
#MySQL数据库驱动
jdbc.url=jdbc:mysql://localhost:3306/ssh_test?useUnicode=true&characterEncoding=UTF-8
#ssh_test为对应的数据库的名称
进入 applicationContext.xml 文件进行对应的数据库配置。
<!--配置连接池属性-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置 c3p0 连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
然后将 hibernate 对应的信息也写入 applicationContext.xml 中,使Spring管理SessionFactory
<!-- 配置Hibernate SessionFactory相关属性 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 加载Hibernate中的映射文件 -->
<property name="mappingResources">
<list>
<value>com/test/ssh/domain/Employee.hbm.xml</value>
<value>com/test/ssh/domain/Department.hbm.xml</value>
</list>
</property>
</bean>
点击Project Structure, 《------Modules, "+" 选择 Hibernate
然后 让idea 链接数据库,点击左下角的胶卷,
最右边弹出框,点击DataBase
"+" <---- "DataSource" <--------MySQL, <------ 不选database, 只输入账号密码,test,apply, 然后"+"<------Schema,ssh_test即可
4 .构建Web模块,并通过 hibernate框架 自动建表
File<-- Project Structure <-- Modules<---“+” <---- Web 然后添加 Web ,然后fix(如果有感叹号警告),IDEA自动创建 web 目录和WEB -INF 和 web.xml
并将 Spring 的过滤器放入 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- spring容器配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring容器监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
</web-app>
新建一个login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
</head>
<body>
This is login.jsp
</body>
</html>
5。运行建表点击Edit Configurations ,
“+” <----- Tomcat <-----local <----- fix
然后运行Tomcat即可建表。
看到页面 login.jsp 即成功,查看数据库。
建表成功。
Spring 和 Hibernate 整合完毕。