<servlet>
<servlet-name>erp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--
处理
base
问题
-->
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.bdqn.erp.web.InitServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping><?xml-stylesheet type="text/xsl" href=""?>
<servlet-name>erp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
整合
spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
WebRoot\WEB-INF\erp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.bdqn.erp.web"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
WebRoot\WEB-INF\applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan
base-package="com.bdqn.erp.dao.jpa,com.bdqn.erp.service.impl" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/erp"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="
entityManagerFactory
"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL"></property>
<property name="generateDdl" value="false"></property>
<property name="showSql" value="true"></property>
</bean>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(*
com.bdqn.erp.service.impl.*.*(..))"/>
</aop:config>
</beans>
springMVC
项目分层
web
中存放
controller
自定义
servlet
service
中存放
service
entity
文件夹中存放实体
dao
层存放
dao
工号
15186
密码
ouxiang130
servlet
的方法
init
()
在
web.xml
中配置后该方法在
web
项目运行后就会被自动加载
用到的注解有
实体类中
@Entity
@Table(name="erp_product")
@Id
@GenericGenerator(name="IdGenerator" , strategy="uuid")
@GeneratedValue(generator="IdGenerator")
@ManyToOne
@JoinColumn(name="typeid")
@ManyToMany
@JoinTable(name="erp_user_role",
joinColumns = @JoinColumn(name="user_id"),
inverseJoinColumns = @JoinColumn(name="role_id"))
private Set<Role> roles = new HashSet<Role>();
@OneToMany(cascade=CascadeType.PERSIST,mappedBy="customer")
//
@JoinColumn(name="customer")
//
@Transient
不持久化的属性
命名试
hql
sql
查询
@NamedQueries({
@NamedQuery(name="findAllEmps" , query = "from Employee"),
@NamedQuery(name="findEmpByName" , query = "from Employee where name=?1")
})
@NamedNativeQuery(name="findAllEmpsSQL" ,
query="select * from erp_employee",
resultClass = Employee.class
)
Dao
层
@Repository
@PersistenceContext
private EntityManager em
@Repository(value="userJpaDAO")
service
层
@Service
public class ProdTypeServiceImpl implements ProdTypeService {
@Resource
private ProdTypeDAO prodTypeDAO;
web
层
@Controller
@RequestMapping("product")
public class ProductController {
@Resource
private ProductService productService;
@RequestMapping( value="add" , method = RequestMethod.GET)
public String add(Model model){
test
包
private EntityManager em
@BeforeClass
public static void createEntityManagerFactory(){
factory = Persistence.createEntityManagerFactory("erp");
}
@Before
public void createEntityManager(){
em = factory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
}
@After
public void commit(){
em.getTransaction().commit();
}
@Test
public void testGetEmps(){