The architecture of the CRM project
* Create a Web project
* Introduce Jar Pack
* MySQL driver package
* Hibernate required Jar Pack
* Spring needs a jar pack
* Struts2 Required Jar Pack
* Jstl Required Jar Pack
* JUnit Required jar Packs
* Introduction to the Package
* Cn.itcast.crm.container: Re-encapsulating Spring container
* Cn.itcast.crm.dao: The DAO interface is placed
* Cn.itcast.crm.dao.impl: The implementation class of the DAO interface is placed
* Cn.itcast.crm.domain: Placed in the Po class, PO Class and database table associated with the mapping file
* Cn.itcast.crm.service: interface of the business layer
* Cn.itcast.crm.service.impl: Implementation class for interface of business layer
* CN.ITCAST.CRM.WEB.ACTION:STRUTS2 's action
* Cn.itcast.crm.web.form: JavaBean to encapsulate page data
* JUnit: Developer test
* Build Hibernate layer
* Define requirements: page data for departmental information to be inserted into the database
* Definition Table
CREATE TABLE ' Sys_user_group ' (
' ID ' INTEGER (one) not NULL auto_increment, #编号
' Remark ' TEXT, #备注
' Name ' VARCHAR (MB) DEFAULT NULL, #部门名称
' Principal ' VARCHAR DEFAULT NULL, #部门负责人
' Incumbent ' VARCHAR DEFAULT NULL, #部门职能
PRIMARY KEY (' id ')
)
* Create PO object, placed under Cn.itcast.crm.domain package
public class Sysusergroup implements Java.io.Serializable {
Private Integer ID;
Private String remark; Note
private String name; Department Name
Private String principal; Department heads
Private String incumbent; Departmental functions
}
* Create a mapping file between the table and the PO SysUserGroup.hbm.xml placed under the Cn.itcast.crm.domain package
* SysUserGroup.hbm.xml the contents of the file directly look at the file can be
* Create Hibernate.cfg.xml File connection database, load SysUserGroup.hbm.xml file, place src under
<session-factory>
<property name= "Hibernate.connection.username" >root</property>
<property name= "Hibernate.connection.password" >root</property>
<property name= "Hibernate.connection.url" >jdbc:mysql://localhost:3306/itcast0807crm</property>
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Hibernate.connection.autocommit" >true</property>
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQL5Dialect</property>
<property name= "Hibernate.hbm2ddl.auto" >update</property>
<property name= "Hibernate.show_sql" >true</property>
<mapping resource= "Cn/itcast/crm/domain/sysusergroup.hbm.xml"/>
</session-factory>
* Test. Placed under JUnit package
public class Testhibernate {
@Test
public void testhibernateconf () {
Configuration config=new Configuration ();
Config.configure ();
Sessionfactory sf=config.buildsessionfactory ();
Session s=sf.opensession ();
Transaction tx=s.begintransaction ();
Sysusergroup sysusergroup=new Sysusergroup ();
Sysusergroup.setname ("sales department");
Sysusergroup.setprincipal ("xxx");
Sysusergroup.setincumbent ("TTT");
S.save (Sysusergroup);
Tx.commit ();
S.close ();
}
}
* Build Spring and Hibernate
* Create Beans.xml file, placed under SRC
* Introduce namespace Bean TX context AOP
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
* Configuration is as follows:
<!--1 Automatic scan--> for configuration annotations
<context:component-scan base-package= "Cn.itcast.crm"/>
<!--3 Configure localization Agent engineering Bean, which is the portal of spring consolidation hibernate-->
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "Configlocation" >
<!--represents loading Hibernate.cfg.xml under Classpath-->
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!--4 Creating the transaction manager AOP Slice-->
<bean id= "Txmanager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>
<!--5 annotations for configuration processing transactions-->
<tx:annotation-driven transaction-manager= "Txmanager"/>
* Create a common interface for the DAO layer, placed under Cn.itcast.crm.dao
Public interface Icommondao<t> {
public void Save (T entity);
}
* Create the implementation class for the DAO layer common interface, placed under Cn.itcast.crm.dao.impl
public class Commondaoimpl<t> extends Hibernatedaosupport implements icommondao<t> {
public void Save (T entity) {
This.gethibernatetemplate (). Save (entity);
}
@Resource (name= "sessionfactory")//injection Sessionfactory
public void Setsessionfactorydi (Sessionfactory sessionfactory) {
System.out.println ("Sessionfactory" +sessionfactory);
Invokes the Setsessionfactory method of the parent class, injecting the Sessionfactory
Super.setsessionfactory (sessionfactory);
}
}
* Create a Departmental DAO interface Sysusergroupdao interface placed under Cn.itcast.crm.dao package
Public interface Isysusergroupdao extends icommondao<sysusergroup> {
Public final static String service_name= "Cn.itcast.crm.dao.impl.SysUserGroupDaoImpl";
}
* The implementation class of the DAO interface that created the department is placed under the Cn.itcast.crm.dao.impl package
@Repository (Isysusergroupdao.service_name)
public class Sysusergroupdaoimpl extends commondaoimpl<sysusergroup> implements Isysusergroupdao {
}
* Test, placed under JUnit package
public class Testsysusergroupdao {
@Test
public void Testsave () {
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Beans.xml");
Isysusergroupdao Sysusergroupdao = (Isysusergroupdao) Ctx.getbean (isysusergroupdao.service_name);
Sysusergroup Sysusergroup = new Sysusergroup ();
Sysusergroup.setname ("sales department");
Sysusergroup.setprincipal ("xxx");
Sysusergroup.setincumbent ("TTT");
Sysusergroupdao.save (Sysusergroup);
}
}
* Create a departmental business layer interface, placed under the Cn.itcast.crm.service package
Public interface Isysusergroupservice {
Public final static String service_name= "Cn.itcast.crm.service.impl.SysUserGroupServiceImpl";
public void Savesysusergroup (Sysusergroup sysusergroup);
}
* Create a Departmental business layer interface implementation class, placed under the Cn.itcast.crm.service.impl package
@Transactional (Readonly=true)
@Service (Isysusergroupservice.service_name)
public class Sysusergroupserviceimpl implements Isysusergroupservice {
@Resource (Name=isysusergroupdao.service_name)
Private Isysusergroupdao Sysusergroupdao;
@Transactional (Isolation=isolation.default,propagation=propagation.required,readonly=false)
public void Savesysusergroup (Sysusergroup sysusergroup) {
Sysusergroupdao.save (Sysusergroup);
}
}
* Test
public class Testsysusergroupservice {
@Test
public void Testsavex () {
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Beans.xml");
Isysusergroupservice Sysusergroupservice = (isysusergroupservice) Ctx.getbean (isysusergroupservice.service_name);
Sysusergroup Sysusergroup = new Sysusergroup ();
Sysusergroup.setname ("sales department");
Sysusergroup.setprincipal ("xxx");
Sysusergroup.setincumbent ("TTT");
Sysusergroupservice.savesysusergroup (Sysusergroup);
}
}
* Build STRUTS2 (save Department information)
* JSP page (sys/group/add.jsp)
* The requested path/sys/sysusergroupaction_save.do
* Create sysusergroupaction based on path and add Save method in action, placed under Cn.itcast.crm.web.action package
public class Sysusergroupaction extends Actionsupport
Public String Save () throws Illegalaccessexception, invocationtargetexception{
System.out.println ("xxxxxxxxxxxxxxxxxxxxxxxx");
return null;
}
}
* Establish an association between the request path and the action
* Create Struts.xml file, placed under SRC
* The contents of the document are as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts Public
"-//apache Software foundation//dtd Struts Configuration 2.1.7//en"
"Http://struts.apache.org/dtds/struts-2.1.7.dtd" >
<struts>
<!--configuration request suffix name. Do-->
<constant name= "struts.action.extension" value= "Do"/>
<!--configuration topics are simple topics-->
<constant name= "Struts.ui.theme" value= "simple"/>
<!--configuration struts2 mode for development mode-->
<constant name= "Struts.devmode" value= "true"/>
<package name= "sys" namespace= "/sys" extends= "Struts-default" >
<action name= "sysusergroupaction_*" class= "cn.itcast.crm.web.action.SysUserGroupAction" method= "{1}" >
<result name= "Add" >/sys/group/add.jsp</result>
</action>
</package>
</struts>
* Configure STRUTS2 filter in Web.xml file
<filter>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
* Test
* Define JavaBean (Vo object) to receive form data, placed under Cn.itcast.crm.web.form package
public class Sysusergroupform implements Java.io.Serializable {
Private String ID;
private String name;
Private String principal;
Private String incumbent;
Private String remark;
}
* Action to implement model-driven interface
public class Sysusergroupaction extends Actionsupport implements modeldriven<sysusergroupform>{
Private Sysusergroupform sysusergroupform=new sysusergroupform ();
Public String Save () throws Illegalaccessexception, invocationtargetexception{
System.out.println ("xxxxxxxxxxxxxxxxxxxxxxxx");
return null;
}
Public Sysusergroupform Getmodel () {
return sysusergroupform;
}
}
* Test, Sysusergroupform whether to receive the value
* How to get the object of the Bean node in the spring container in the Save method in the Struts2 sysusergroupaction
* Create Serviceprovindercore, load Beans.xml file, place under Cn.itcast.crm.container package
public class Serviceprovindercore {
protected ApplicationContext CTX;
/**
* @param filename Beans.xml
*/
public void Load (String filename) {
Ctx=new classpathxmlapplicationcontext (filename);
}
}
* Create the Serviceprovinder class to get the object that gets the bean node in the spring container, placed under the Cn.itcast.crm.container package
public class Serviceprovinder {
private static Serviceprovindercore SC;
static{
System.err.println ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
Sc=new Serviceprovindercore ();
Sc.load ("Beans.xml");
}
public static Object GetService (String beanname) {
System.err.println ("PPPPPPPPPPPPPPPPPPPPPPP");
if (Stringutils.isblank (Beanname)) {
throw new RuntimeException ("The service name you want to access cannot be empty");
}
Object Bean=null;
If the spring container contains Beanname
if (Sc.ctx.containsBean (Beanname)) {
Bean=sc.ctx.getbean (Beanname);
}
If the spring container does not contain beanname
if (bean==null) {
throw new RuntimeException ("The service name you want to access [" +beanname+] does not exist ");
}
return bean;
}
}
* Test
@Test
public void Testsave () {
Isysusergroupservice sysusergroupservice= (isysusergroupservice) Serviceprovinder.getservice ( Isysusergroupservice.service_name);
Sysusergroup Sysusergroup = new Sysusergroup ();
Sysusergroup.setname ("sales department");
Sysusergroup.setprincipal ("xxx");
Sysusergroup.setincumbent ("TTT");
Sysusergroupservice.savesysusergroup (Sysusergroup);
}
* Add the following code in the Save method in Sysusergroupaction
Public String Save () throws Illegalaccessexception, invocationtargetexception{
System.out.println ("Sysusergroupform.getname ()" +sysusergroupform.getname ());
Instantiate a PO Object
Sysusergroup sysusergroup=new Sysusergroup ();
Value of the assigned Vo object to the PO
Beanutils.copyproperties (Sysusergroup, sysusergroupform);
Gets the object of the business layer (this project struts2 and spring are separate)
Isysusergroupservice sysusergroupservice=
(Isysusergroupservice) Serviceprovinder.getservice (Isysusergroupservice.service_name);
Call the business layer to save the PO object
Sysusergroupservice.savesysusergroup (Sysusergroup);
return null;
}
Test
Related code Download