Since the company is currently using Spring + Spring MVC+ Hibernate, so build a similar framework first.

1. The general structure of a new project is as follows: I will not explain the specific layers: some related JAR packages may be redundant, and various JAR packages are missing at the beginning

2. The second step is to start configuring the relevant configuration files, such as web.xml and the two XML files shown above. (1)web.xml

<display-name>LdyBase</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

<! Load the applicationContext configuration file. You can also add classpath

The sign then goes to get the prefix +

Get all –>


<context-param>


<param-name>contextConfigLocation</param-name>


<param-value>classpath:config/applicationContext.xml</param-value>


</context-param>

< listener > < listener – class > org. Springframework. Web. Context. ContextLoaderListener < / listener – class >


DispatcherServlet


org.springframework.web.servlet.DispatcherServlet

contextConfigLocation classpath:config/springMvc.xml


1



dispatcherServlet


*.do


CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding UTF-8

false

forceResponseEncoding true


CharacterEncodingFilter


/*


openSession


org.springframework.orm.hibernate4.support.OpenSessionInViewFilter



openSession


/*


(2) The springmvc. XML file is as follows (annotated scan, followed by the view parser). I’m used to using HTML, so the parser is configured with HTML

in Java file < MVC :annotation-driven />

< bean class = “org. Springframework. Web. Servlet. The InternalResourceViewResolver” > < property name = “prefix” value=”/html/”>

(3) ApplicationContext.xml

context:component-scan base-package=”com.ldy” />

< bean id = “dataSource” class = “. Org. Springframework. JDBC dataSource. DriverManagerDataSource “> < property name=”driverClassName” value=”com.mysql.jdbc.Driver” />

< bean id = “SessionFactory” class = “org. Springframework. Orm. Hibernate4. LocalSessionFactoryBean” >
com.ldy.model.User
org.hibernate.dialect.MySQLDialect update true true true

< bean id = “transactionManager” class = “org. Springframework. Orm. Hibernate4. HibernateTransactionManager” >

3. After configuring the XML file, simple code is left. (1) Useraction.java package com.dy. Action;

import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.ldy.model.User; import com.ldy.service.UserService; / * *

  • The user Action layer interacts with the front end
  • @author ldy

* */ @Controller @RequestMapping(“/user”) public class UserAction { @Autowired private UserService userService; @requestMapping (value = “/addUser.do”) public String addUser(HttpServletRequest Request) throws Exception {// Obtain user information. Try {String name = request.getParameter(“name”); String password = request.getParameter(“password”); int age = Integer.parseInt(request.getParameter(“age”)); System.out.println(name+” “+password+” “+age); User user=new User(); user.setName(name); user.setAge(age); user.setPassword(password); userService.addUser(user); return “/success”; } catch (Exception e) { e.printStackTrace(); return “/error”; }}}

(2) UserService. Java and UserServicePl. Java are written together with package com.ldy.service; import com.ldy.model.User; / * *

  • User logic processing interface layer
  • @author ldy

*/

public interface UserService {



public void addUser(User user);

}

package com.ldy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.ldy.dao.UserDao;

import com.ldy.model.User;

import com.ldy.service.UserService;

/ * *

  • User logic processing implementation layer
  • @author ldy

*/ @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @transactional public void addUser(user user){userdao.adduser (user); }}

(3) UserDao. Java and Userdaopl. Java are written together with package com.ldy.dao; import com.ldy.model.User;

/ * *

  • User data interaction interface layer
  • @author ldy

*/

public interface UserDao {

public void addUser(User user);

}

package com.ldy.dao.impl;

import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.springframework.stereotype.Repository; import com.ldy.dao.UserDao; import com.ldy.model.User; / * *

  • User data interaction implementation layer
  • @author ldy

*/ @repository public class UserDaoImpl implements UserDao {// Implements the sessionFactory already configured in applicationContext.xml @Resource(name = “sessionFactory”) private SessionFactory sessionFactory; / * * * * @ param add user data and user object * / public void addUser (user user) {sessionFactory. GetCurrentSession (), save (user); } /** * @return user user object set (reserved) */ public List< user > getUsers(){String HQL =”from user”; Query query=sessionFactory.getCurrentSession().createQuery(hql); return query.list(); }}

(4) user.java this is the database corresponding entity class. I didn’t configure the database mapping file hbm.xml because I annotated the entity class directly to correspond to the database, which felt easier. Then my user table id is varchar type is the system generated 32 bit UUID package com.ldy.model;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; / * *

  • User entity class
  • @author ldy

* */ @Entity @Table(name = “user”) public class User { @Id @Column(name = “id”) @GeneratedValue(generator = “system-uuid”) @GenericGenerator(name = “system-uuid”, strategy = “uuid”) private String id ; @Column(name = “name”) private String name; @Column(name = “password”) private String password; @Column(name = “age”) private int age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}

4. The HTML page. HTML page: 1. adduser.html

< HTML >

Add user

user



Password
< br / > age < input type = “text” name = “age” / > < br / > < input type = “submit” value = “registered” > < / form > < / body > < / HTML >

2.error.html

< HTML >

</body> </html>

3.success.html

< HTML >

Insert title here

At this point, the Spring + PringMVC + Hibernate framework is built successfully.

I have been directly developing with the framework of the company in the past. I have encountered various problems this time. I haven’t remembered some minor problems, but I have remembered several major ones. The study.

Question 1: Java. Util. Concurrent. ExecutionException: org. Apache. Catalina. LifecycleException: Failed to start component [StandardEngine[Catalina].standardHost [localHost].StandardContext[/LdyBase]] This problem is simple, method rebuild path to choose the appropriate JDK. Lack of jars

Question 2: Could not autowire field: private com. Ldy. Service. UserService com. Ldy. Action. UserAction. UserService; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Always find a class boPooledDataSource [com.mchange.v2.c3p0.Com] for bean with the name ‘dataSource’ defined in the class path resource [config/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: Com.mchange.v2.c3p0.Com boPooledDataSource c3p0 missing jars, looking for the main time jars, and then found a web site to look for, can’t say that, but most are. Attach links to cool network question 3: sessionFactory. GetCurrentSession (). The save (user); The code executed without any problems, but the database did not save successfully. Solution: 1. This time or catch the exception, and then print the problem. 2. Configuration Settings when SessionFactory autocommit < prop key = “hibernate. Connection. The autocommit mode” > true < / prop > The Transactional annotation @Transactional should be added to the Service layer before the SAVE () method in the DAO layer is called, and also to the Transactional annotation when operating ona database

It is a little simple because it just says that I want to see how I can build a Spring + PringMVC + Hibernate framework, so as long as I can achieve data insertion on the line. In the afternoon, WE are building a framework of Spring + PringMVC + Mybatis.