一、搭建矿建
1、整体的结构:
2、jar包引入,此处使用maven
junit
junit
4.10
javax.servlet
javax.servlet-api
3.1.0
org.springframework
spring-webmvc
4.1.3.RELEASE
org.springframework
spring-context
4.1.3.RELEASE
org.springframework
spring-beans
4.1.3.RELEASE
org.springframework
spring-web
4.1.3.RELEASE
org.springframework
spring-jdbc
4.1.3.RELEASE
org.springframework
spring-aop
4.1.3.RELEASE
org.springframework
spring-tx
4.1.3.RELEASE
org.springframework
spring-core
4.1.3.RELEASE
org.springframework
spring-orm
4.1.3.RELEASE
org.hibernate
hibernate-core
4.3.11.Final
org.hibernate
hibernate-c3p0
4.3.5.Final
org.hibernate
hibernate-entitymanager
4.3.11.Final
org.hibernate
hibernate-ehcache
4.3.11.Final
mysql
mysql-connector-java
5.1.35
commons-dbcp
commons-dbcp
1.2.2
javax.servlet
jstl
1.2
org.slf4j
slf4j-api
1.7.12
com.jolbox
bonecp
0.8.0.RELEASE
com.jolbox
bonecp-spring
0.8.0.RELEASE
com.jolbox
bonecp-provider
0.8.0-alpha1
taglibs
standard
1.1.2
org.aspectj
aspectjweaver
1.8.7
cglib
cglib-nodep
3.2.0
commons-collections
commons-collections
3.2.1
ant
ant
1.6.5
org.apache.avro
avro
1.7.7
commons-beanutils
commons-beanutils
1.9.2
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
org.apache.commons
commons-lang3
3.4
commons-codec
commons-codec
1.9
dom4j
dom4j
1.6.1
net.sf.ezmorph
ezmorph
1.0.6
org.freemarker
freemarker
2.3.23
com.google.guava
guava
18.0
org.jboss.logging
jboss-logging
3.3.0.Final
com.fasterxml.jackson.core
jackson-databind
2.5.0
com.fasterxml.jackson.core
jackson-core
2.5.0
com.fasterxml.jackson.core
jackson-annotations
2.5.0
org.springframework.data
spring-data-jpa
1.7.1.RELEASE
com.jcraft
jsch
0.1.46
log4j
apache-log4j-extras
1.1
org.apache.tiles
tiles-servlet
3.0.3
org.apache.tiles
tiles-jsp
3.0.3
org.springframework
spring-test
4.3.3.RELEASE
org.aspectj
aspectjrt
1.8.9
cglib
cglib
2.2.2
javax.mail
mail
1.4.7
com.itextpdf
itextpdf
5.5.8
com.itextpdf
itext-pdfa
5.5.8
com.itextpdf
itext-xtra
5.5.8
com.itextpdf.tool
xmlworker
5.5.8
com.lowagie
itext
2.1.7
com.kenai.nbpwr
org-apache-batik-all
1.7-201003011305
org.eclipse.birt.runtime
org.w3c.dom.smil
1.0.1.v200903091627
org.eclipse.birt.runtime.3_7_1
org.w3c.dom.svg
1.1.0
3、web.xml的创建
LibraryManage
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.util.IntrospectorCleanupListener
org.springframework.web.context.ContextLoaderListener
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
*.do
/index.jsp
3、applicationContent.xml的创建
thread
update
true
false
4、spring-mvc.xml的创建
classpath:titles.xml
org.springframework.web.servlet.view.tiles3.TilesView
5、jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/libManage?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
jdbc.password=123456
6、log4j.properties
log4j.rootLogger = INFO,CONSOLE,logFile
log4j.logger.org.hibernate = INFO,CONSOLE,logFile
log4j.addivity.org.apache = true
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern ==%d{yyyy-MM-dd HH:mm:ss,SSS} %p %l -%m%n
log4j.appender.logFile=org.apache.log4j.FileAppender
log4j.appender.logFile.ImmediateFlush=true
log4j.appender.logFile.Append=true
log4j.appender.logFile.File=D:/logs/testH/log.log4j
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
至此,基本框架搭建好。
二、创建类,使用junit测试
1、创建service类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class TestService {
@Autowired
private TestDao testDao;
@Transactional
public void testSave() throws Exception{
Test test = new Test();
test.setPassword("sdfsad");
test.setUserName("qwerdddd");
testDao.testSave(test);
}
}
2、创建dao类
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
@Repository
public class TestDao extends BaseDao {
public void testSave(Test test) {
em.persist(test);
em.flush();
System.out.println("save success");
}
}
其中BaseDao为:
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.Session;
public abstract class BaseDao {
@PersistenceContext
protected EntityManager em;
protected EntityManager getEm() {
return em;
}
protected void setEm(EntityManager em) {
this.em = em;
}
/**
* Insert entity
*
* @param obj
* @return
* @throws Exception
*/
public boolean save(T obj) throws Exception {
em.persist(obj);
em.flush();
return true;
}
public boolean merge(T obj) throws Exception {
em.merge(obj);
em.flush();
return true;
}
/**
* Insert entity
*
* @param obj
* @return
* @throws Exception
*/
public T saveObj(T obj) throws Exception {
em.persist(obj);
em.flush();
return obj;
}
/**
* Get entity
*
* @param clazz
* @param id
* @return
* @throws Exception
*/
public T get(Class clazz, Object id) throws Exception {
T obj = em.find(clazz, id);
return obj;
}
/**
* Update entity
*
* @param obj
* @return
* @throws Exception
*/
public boolean update(T obj) throws Exception {
em.merge(obj);
em.flush();
return true;
}
/**
* Delete entity
*
* @param obj
* @return
* @throws Exception
*/
public boolean delete(T obj) throws Exception {
obj = em.merge(obj);
em.remove(obj);
em.flush();
return true;
}
public boolean batchDelete(List entities){
for(int i = 0; i boolean delete(Object id, Class clazz) throws Exception {
T obj = this.get(clazz, id);
if (obj != null) {
this.delete(obj);
return true;
}
return false;
}
@SuppressWarnings("rawtypes")
public void batchInsert(String entityName,List list) {
Session mySession = (Session) em.getDelegate();
for(int i = 0; i < list.size(); i++) {
mySession.save(entityName, list.get(i));
}
em.flush();
em.clear();
}
}
3、创建test主类
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.libm.service.TestService;
public class TestApp {
@Autowired
private TestService testService;
@Before
public void init(){
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml"});
testService = (TestService) context.getBean("testService");
}
@Test
public void saveUser() throws Exception {
System.out.println("this is test save");
testService.testSave();
}
}