Simple spring MVC + jpa configuration case, springmvcjpa case
SpringMVC + jpa was built in person. Looking at the blogs on the internet, I found a lot of errors in setting up the framework. Now let's go through the process for later viewing.
One of the problems I encountered was that it was possible to directly start tomcat to run and save the object, but an error was reported after passing the unit test:
Caused by: javax. validation. ValidationException: Unable to instantiate Configuration.
Solution:
Add this <property name = "javax. persistence. validation. mode" value = "none"/> to persistence. xml,
In the myeclipse installation directory, find the EE_6 directory and remove the bean-validator.jar.
But I still can't figure it out, just delete it, and start tomcat won't, why tomcat will ignore this problem. Please advise. If you have any questions, please advise. Http://www.cnblogs.com/yuanfy008/p/4156287.html)
Step 1: prepare the corresponding jar package. In fact, I don't know which packages to use (I need to learn this ), first, make a rough estimate and add one by one based on the errors.
Step 2: Configure web. xml
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <web-app version = "3.0" 3 xmlns = "http://java.sun.com/xml/ns/javaee" 4 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 5 xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 7 <display-name> </display-name> 8 <welcome-file-list> 9 <welcome-file> index. jsp </welcome-file> 10 </welcome-file-list> 11 <! -- Spring transaction configuration --> 12 <context-param> 13 <param-name> contextConfigLocation </param-name> 14 <param-value> classpath *: spring -*. xml </param-value> 15 </context-param> 16 <listener> 17 <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> 18 </listener> 19 <! -- Full-site encoding filter --> 20 <filter> 21 <filter-name> encodingFilter </filter-name> 22 <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> 23 <init-param> 24 <param-name> encoding </param-name> 25 <param-value> UTF-8 </param-value> 26 </init-param> 27 <init-param> 28 <param-name> forceEncoding </param-name> 29 <param-value> true </param-value> 30 </init-param> 31 </filter> 32 <filter-mapping> 33 <fil Ter-name> encodingFilter </filter-name> 34 <url-pattern>/* </url-pattern> 35 </filter-mapping> 36 37 <! -- SpringMVC configurator --> 38 <servlet> 39 <servlet-name> springMVC </servlet-name> 40 <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> 41 <init-param> 42 <param-name> contextConfigLocation </param-name> 43 <param-value> classpath *: spring-mvc.xml </param-value> 44 </init-param> 45 <load-on-startup> 1 </load-on-startup> 46 </servlet> 47 <servlet- mapping> 48 <servlet-name> springMVC </servlet-name> 49 <url-pattern>/</url-pattern> 50 </servlet-mapping> 51 </web- app>
Step 3: configure the xml file spring-orm.xml related to hibernate
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xmlns: context = "http://www.springframework.org/schema/context" 5 xmlns: aop = "http://www.springframework.org/schema/aop" 6 xmlns: tx = "http://www.springframework.org/schema/tx" 7 xsi: schemaLocation = "http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/bea Ns/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 15 16 <! -- Configuring the data source does not use persistence. xml --> 17 <bean id = "dataSource" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> 18 <property name =" driverClassName "value =" com. mysql. jdbc. driver "/> 19 <property name =" url "value =" jdbc: mysql: // localhost: 3306/test? CharacterEncoding = UTF-8 "/> 20 <property name =" username "value =" root "/> 21 <property name =" password "value =" root "/> 22 </bean> 23 24 <! -- Configure EntityManagerFactory --> 25 <bean id = "entityManagerFactory" class = "org. springframework. orm. jpa. localContainerEntityManagerFactoryBean "> 26 <property name =" persistenceUnitName "value =" test "/> 27 <property name =" persistenceXmlLocation "value =" classpath: META-INF/persistence. xml "> </property> 28 </bean> 29 30 <! -- Configure the jpa Transaction Manager --> 31 <bean id = "transactionManager" class = "org. springframework. orm. jpa. jpaTransactionManager "> 32 <property name =" entityManagerFactory "ref =" entityManagerFactory "> </property> 33 </bean> 34 35 <tx: annotation-driven transaction-manager = "transactionManager"/> 36 </beans>
Step 4: configure the jpa configuration file persistence. xml
<?xml version="1.0" encoding="UTF-8"?><persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="test" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="root" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="false" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="javax.persistence.validation.mode" value="none" /> </properties> </persistence-unit></persistence>
Step 5: Configure springMVC-related configuration file, spring-mvc.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework.org/schema/context" xmlns: p = "http://www.springframework.org/schema/p" xmlns: mvc = "http://www.springframework.org/schema/mvc" 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-3. 0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <! -- Two of the following annotation classes can be annotated. --> <mvc: annotation-driven/> <! -- File Upload configuration --> <bean id = "multipartResolver" class = "org. springframework. web. multipart. commons. commonsMultipartResolver "> <property name =" defaultEncoding "value =" UTF-8 "/> <property name =" maxUploadSize "value =" 10485760000 "/> <property name =" maxInMemorySize "value = "40960"/> </bean> <! -- Static resource access configuration --> <mvc: resources location = "/img/" mapping = "/img/**"/> <mvc: resources location = "/topui/" mapping = "/topui/**"/> <! -- View parser --> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/"> </property> <property name =" suffix "value = ". jsp "> </property> </bean> </beans>
Step 6: Use a general file to include these configuration files. During unit testing, there will be fewer configuration files. Spring-sevlet.xml
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework.org/schema/context" xmlns: p = "http://www.springframework.org/schema/p" xmlns: mvc = "http://www.springframework.org/schema/mvc" 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-3 . 0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <context: annotation-config/> <! -- Annotation scan package --> <context: component-scan base-package = "com. bank. * "/> <import resource =" spring-mvc.xml "/> <import resource =" spring-orm.xml "/> </beans>
After the corresponding configuration is complete, we will write an object class for unit testing. Let's take user as an example.
@Entitypublic class User { @Id @GeneratedValue(generator = "uuidGenerator") @GenericGenerator(name = "uuidGenerator", strategy = "uuid") @Column(length = 32, nullable = false) private String userId; private String userName; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; }}
The corresponding dao layer IUserDAO is as follows:
public interface IUserDAO { public void save(User user);}
The corresponding UserDAO is as follows:
@Repositorypublic class UserDAO implements IUserDAO {@PersistenceContext(unitName="test")private EntityManager entityManager;@Transactional(rollbackFor = Exception.class)@Overridepublic void save(User user) {//System.out.println(entityManagerFactory);this.entityManager.persist(user);System.out.println("---"+user.getUserId());}}
Then, the typical unit test is as follows:
@ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration (locations = {"classpath: spring-servlet.xml"}) @ TransactionConfiguration (defaultRollback = false) public class UserTest {@ Resource private IUserDAO userDAO; @ Before public void start () {System. out. println ("Test start ---") ;}@ After public void end () {System. out. println ("Test over ---") ;}@ Test public void test1 () {User user = new User (); user. setUserName ("test"); userDAO. save (user); // System. out. println (userDAO );}}
The test output is as follows:
Test start ---
--- 2c92de9e4a340533014a340537290000
Hibernate: insert into User (userName, userId) values (?, ?)
Test over ---