maven+springmvc+hibernate4框架搭建-纯手工亲手测试通过
程序员文章站
2024-03-18 22:55:40
...
一:创建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.0com.mjsspringmvctestwar0.0.1-SNAPSHOTspringmvctest Maven Webapphttp://maven.apache.orgspringmvctestorg.apache.maven.pluginsmaven-compiler-plugin2.3.21.81.8junitjunit3.8.1testorg.springframeworkspring-context4.1.0.RELEASEorg.aspectjaspectjrt1.8.0aspectjaspectjweaver1.5.3org.springframeworkspring-webmvc4.1.0.RELEASEorg.springframeworkspring-orm4.1.1.RELEASEorg.hibernatehibernate-core4.3.6.Finalcommons-dbcpcommons-dbcp1.4mysqlmysql-connector-java5.1.18javax.servletjavax.servlet-api3.0.1providedjavax.servletjstl1.1.2providedjavax.servlet.jspjavax.servlet.jsp-api2.3.1provided
最好是用自己下载的maven软件,然后导入使用:
四:在src/main/resources目录下新建:
applicationContext.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
org.hibernate.dialect.MySQLDialecttrueupdate,createtrue
在相同目录下再新建:
springmvc-servlet.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
最后解决工程配置小错误的问题,前提是源码全部编译通过。
点击项目目录->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就能发现后台数据库有一条数据更新。