构建JPA工程J2EE 5 只支持JPA1.0 ;要构建JPA2.0 要j2EE6 现在支持开源的应用服务器不多 只有glassfish;构建的报需要如下
这些包我不上传了官方都有。(如果觉得有些包比较难找 可以用www.findjar.com搜索)
persistence.xml 中的配置为:
<persistence-unit name="tcsdatasource" transaction-type="RESOURCE_LOCAL">
<properties>
<!-- 数据库方言-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<!-- 数据库用户名 -->
<property name="hibernate.connection.username" value="admins"/>
<!-- 数据库密码 -->
<property name="hibernate.connection.password" value="root"/>
<!-- 数据库连接URL -->
<property name="hibernate.connection.url"
value="jdbc:mysql://10.6.0.6:3306/tcscloud?useUnicode=true&characterEncoding=UTF-8"/>
<!-- 最大抓取深度 -->
<property name="hibernate.max_fetch_depth" value="3"/>
<!-- 更新方式创建库表 -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- 显示SQL -->
<property name="hibernate.show_sql" value="true"/>
<!-- 格式SQL -->
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
spring 的配置文件如下:
<context:annotation-config/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="tcsdatasource"/>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
struts 配置文件(实现了自定意拦截器):
<struts>
<constant name="dev.model" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.convention.default.parent.package" value="project-default" /> <!--设置默认的包-->
<constant name="struts.convention.package.locators" value="action" />
<package name="project-default" extends="convention-default">
<!--拦截器配置-->
<interceptors>
<!--登录拦截器-->
<interceptor name="loginInterceptor" <!--spring 配置文件自定意的类-->
class="loginInterceptor">
</interceptor>
<!--权限拦截器-->
<interceptor name="permissionInterceptor" <!--spring 配置文件自定意的类-->
class="permissionInterceptor">
</interceptor>
<interceptor-stack name="loginDefaultStack">
<interceptor-ref name="loginInterceptor"></interceptor-ref>
<interceptor-ref name="permissionInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginDefaultStack"></default-interceptor-ref>
<!--全局返回配置-->
<global-results>
<result name="error">/pages/common/error.jsp</result>
<result name="sessionTimeout">/pages/common/sessiontimeout.jsp</result>
</global-results>
</package>
</struts>
DAO的EntityManager 注入:
/**
*@see 框架测试的Dao
*@author desert
*@version 1.0
*/
public class TestDaoImpl implements TestDao{
@PersistenceContext
private EntityManager em;
public void saveTest(TestBean testBean){
this.em.persist(testBean);
}
....................
}
事务处理的DEMO:
@Transactional
public class TestServiceImpl implements TestService{
@Resource
private TestDao testDao;
@Transactional(propagation = Propagation.REQUIRED)
public TestBean getTestBeanById(int id){
return this.testDao.getTestById(id);
}
// @Transactional(propagation = Propagation.REQUIRED)
public void removeTestBean(TestBean testBean){
this.testDao.removeTest(testBean);
}
// @Transactional(propagation = Propagation.REQUIRED)
public void saveTestBean(TestBean testBean)throws Exception{
try{
this.testDao.saveTest(testBean);
this.testDao.getTestBeanList();
}catch(Exception e){
throw new TcsException(e.getMessage());
}
}
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public List<TestBean> getTestBeanList()throws Exception{
List<TestBean> list = null;
list = this.testDao.getTestBeanList();
return list;
}
}
测试代码编写:
/**
*@see 框架测试的Junit
*@author desert
*@version 1.0
*/
public class JunitTest {
private static TestService testService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext-service.xml","/springconfig/*-service.xml"});
testService = (TestService) applicationContext.getBean("testService");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
@Test
public void testSave()throws Exception{
}
@Test
public void testUpdate() {
}
@Test
public void testGetById() {
/*TestBean object = this.testService.getTestBeanById(1);
System.out.println(object.getUserName()+" "+object.getPassword());*/
}
@Test
public void testDelete() {
}
@Test
public void testGetList()throws Exception{
List<TestBean> testBean_list = testService.getTestBeanList();
for(TestBean object:testBean_list){
System.out.println(object.getUserName());
}
}
}
struts2 注解demo:
/**
*@see 框架测试的TestAction(测试通过)
*@author desert
*@version 1.0
*/
@ParentPackage("project-default") //可以省略不写(设置了默认)
@Namespace("/test") //为了维护方便必须写
@Results({
@Result(name="success",location="/pages/common/sessiontimeout.jsp"),
@Result(name="index",location="/pages/common/error.jsp")
})
public class TestAction extends BaseAction{
private static final long serialVersionUID = 1L;
@Resource
private TestService testService;
public String forwardIndex()throws Exception{
testService.getTestBeanList();
return "index";
}
}
TestAction 去掉Action 并且第一个字母小写得到test做为Action类的访问名
访问路径为:http://工程名/test/test!forwardIndex.action