一:创建maven web工程:
最初建成的工程目录张这样:
新建:
src/main/java;
src/test/java;
src/test/resources;
这三个源码文件夹:
右击工程目录->新建->源码文件夹
二、web工程配置
项目中只显示了一个文件夹,其实已经有了src/main/java和src/test/java。将其显示出来:
完成后出现其他两个目录
添加src/test/resources目录:
右击项目->新建->源码文件夹
要使其他两个文件显示出来,做法如下:
只要添加一个文件就好了:即src/test/resources
三、导入依赖包:
pom文件:
4.0.0
com.mjs
springmvctest
war
0.0.1-SNAPSHOT
springmvctest Maven Webapp
http://maven.apache.org
springmvctest
org.apache.maven.plugins
maven-compiler-plugin
2.3.2
1.8
1.8
junit
junit
3.8.1
test
org.springframework
spring-context
4.1.0.RELEASE
org.aspectj
aspectjrt
1.8.0
aspectj
aspectjweaver
1.5.3
org.springframework
spring-webmvc
4.1.0.RELEASE
org.springframework
spring-orm
4.1.1.RELEASE
org.hibernate
hibernate-core
4.3.6.Final
commons-dbcp
commons-dbcp
1.4
mysql
mysql-connector-java
5.1.18
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet
jstl
1.1.2
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
最好是用自己下载的maven软件,然后导入使用:
四:在src/main/resources目录下新建:
applicationContext.xml文件,内容如下:
org.hibernate.dialect.MySQLDialect
true
update,create
true
在相同目录下再新建:
springmvc-servlet.xml,内容如下:
最后解决工程配置小错误的问题,前提是源码全部编译通过。
点击项目目录->maven->更新项目:
在src/main/java目录下建如下类:
IGeneralDao.java类和GeneralDao.java的内容:
package com.mjs.dao;
import java.io.Serializable;
import java.util.List;
public interface IGeneralDao {
public
T findById(Class
type, Serializable id);
public
List
findAll(Class
type); public void save(Object... entities); public void update(Object... entities); public void saveOrUpdate(Object entity); public void delete(Object... entities); public void deleteById(Class
type, Serializable id); public void refresh(Object... entities); public void flush(); } package com.mjs.dao; import java.io.Serializable; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate4.HibernateTemplate; import org.springframework.stereotype.Repository; @Repository public class GeneralDao implements IGeneralDao { /** * 这个bean里面需要注入sessionFactory,所以把这个bean写在了配置中。 */ @Autowired private HibernateTemplate hibernateTemplate; public
T findById(Class
type, Serializable id) { return hibernateTemplate.get(type, id); } public
List
findAll(Class
type) { return hibernateTemplate.loadAll(type); } public void save(Object... entities) { for (Object entity : entities) { hibernateTemplate.save(entity); } } public void saveOrUpdate(Object entity) { hibernateTemplate.saveOrUpdate(entity); } public void update(Object... entities) { for (Object entity : entities) { hibernateTemplate.update(entity); } } public void delete(Object... entities) { for (Object entity : entities) { if (entity != null) { hibernateTemplate.delete(entity); } } } public void deleteById(Class
type, Serializable id) { if (id == null) { return; } Object entity = findById(type, id); if (entity == null) { return; } delete(entity); } public void refresh(Object... entities) { for (Object entity : entities) { hibernateTemplate.refresh(entity); } } public void flush() { hibernateTemplate.flush(); } }
IUserService.java和UserServcie.java的内容:
package com.mjs.service;
import com.mjs.entity.User;
public interface IUserService {
public boolean regist(User user);
}package com.mjs.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mjs.dao.IGeneralDao;
import com.mjs.entity.User;
@Service
public class UserService implements IUserService {
@Autowired
private IGeneralDao generalDao;
@Transactional(readOnly=false)
public boolean regist(User user) {
generalDao.save(user);
return false;
}
}
TestController.java的内容:
package com.mjs.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mjs.entity.User;
import com.mjs.service.IUserService;
@Controller
public class TestController {
@Autowired
private IUserService userService;
@RequestMapping("/regist")
public String regist(){
User user = new User();
user.setUsername("xiaoma");
user.setPassword("123456");
userService.regist(user);
return "index";
}
}
User.java的内容:
package com.mjs.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User implements Serializable{
@Id
private int id;
private String username;
private String password;
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
接下来,发布到tomcat服务器上,访问localhost:8080/springmvc/regist就能发现后台数据库有一条数据更新。