SpringMVC+Spring+myBatis+JUnit+Maven

wssc198765 贡献于2014-07-03

作者 张师聪  创建于2014-07-03 03:49:00   修改者张师聪  修改于2014-07-03 09:06:00字数15721

文档摘要:SpringMVC+Spring+myBatis+JUnit+Maven
关键词:

目录 SpringMVC+Spring+myBatis+JUnit+Maven 1 1. 工程目录结构 1 2. 引入依赖 2 3. 配置数据库连接 10 4. Spring配置文件 10 5. JAVA代码 15 6. myBatis配置 20 7. JUnit测试 21 8. SpringMVC 23 9. Log4J日志 30 10. 测试 32 11. Controller 32 SpringMVC+Spring+myBatis+JUnit+Maven 1. 工程目录结构 2. 引入依赖 在maven的pom.xml里依次导入 4.0.0 com.jd new_hunter_template war 0.0.1-SNAPSHOT springmvc Maven Webapp http://maven.apache.org 3.2.6.RELEASE 3.2.3 1.6.6 1.2.9 org.springframework spring-core ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-oxm ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-aop ${spring.version} org.springframework spring-context-support ${spring.version} org.springframework spring-aop ${spring.version} org.springframework spring-test ${spring.version} javax.servlet jstl 1.2 javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.2 provided org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring 1.2.2 mysql mysql-connector-java 5.1.28 junit junit 4.11 test com.alibaba druid 1.0.5 org.codehaus.jackson jackson-mapper-asl 1.9.13 log4j log4j ${log4j.version} org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} new_hunter_template 3. 配置数据库连接 conf/ jdbc.properties(jdbc配置文件) jdbc_driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost:3306/nhc?useUnicode=true&characterEncoding=utf-8 jdbc_username=root jdbc_password= 4. Spring配置文件 conf/spring.xml(spring配置文件的扫描) conf/spring-mybatis.xml(spring与mybatis连接属性) ${jdbc_driverClassName} ${jdbc_url} ${jdbc_username} ${jdbc_password} 20 1 60000 20 3 true 180 clientEncoding=UTF-8 5. JAVA代码 com.jd.nhc.dto.UserDto package com.jd.nhc.dto; public class UserDto { private int id; private int state; private String nickname; public UserDto() { } public UserDto(int id, int state, String nickname) { this.id = id; this.state = state; this.nickname = nickname; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getState() { return state; } public void setState(int state) { this.state = state; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @Override public String toString() { return String.format("Id: %d, State: %d, Nickname: %s.", this.id, this.state, this.nickname); } } com.jd.nhc.dao.UserDao package com.jd.nhc.dao; import com.jd.nhc.dto.UserDto; public interface UserDao { /** * 添加新用户 * * @param userDto * @return */ int insertUser(UserDto userDto); } com.jd.nhc.service.UserService package com.jd.nhc.service; import com.jd.nhc.dto.UserDto; public interface UserService { /** * 添加新用户 * * @param userDto * @return */ int insertUser(UserDto userDto); } com.jd.nhc.service.impl.UserServiceImpl package com.jd.nhc.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.jd.nhc.dao.UserDao; import com.jd.nhc.dto.UserDto; import com.jd.nhc.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public int insertUser(UserDto userDto) { return userDao.insertUser(userDto); } } 6. myBatis配置 conf/mybatis-config.xml(mybatis配置的基本文件) mapper/UserMapper.xml(mybatis的实现) insert into t_user( state, nickname) values ( #{state}, #{nickname}) 7. JUnit测试 com.jd.nhc.test.UserTest package com.jd.nhc.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jd.nhc.dto.UserDto; import com.jd.nhc.service.UserService; public class UserTest { private UserService userService; @Before public void before() { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath:conf/spring.xml", "classpath:conf/spring-mybatis.xml" }); userService = (UserService) context.getBean("userServiceImpl"); } @Test public void addUser() { UserDto userDto = new UserDto(); userDto.setNickname("张三"); userDto.setState(1); System.out.println(userService.insertUser(userDto)); } } 测试结果如图: 8. SpringMVC web.xml(web功能配置) Archetype Created Web Application contextConfigLocation classpath:conf/spring.xml; classpath:conf/spring-mybatis.xml webAppRootKey springmvc.root SpringEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true SpringEncodingFilter /* log4jConfigLocation classpath:conf/log4j.properties log4jRefreshInterval 6000 org.springframework.web.util.Log4jConfigListener org.springframework.web.context.ContextLoaderListener spring org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:conf/spring-mvc.xml 2 spring *.do index.jsp 404 /WEB-INF/errorpage/404.jsp 405 /WEB-INF/errorpage/405.jsp 500 /WEB-INF/errorpage/500.jsp conf/spring-mvc.xml(mvc配置文件) text/html;charset=UTF-8 9. Log4J日志 ### set log levels ### log4j.rootLogger = debug , stdout , D ### output to the console ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n ### Output to the log file ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = ${springmvc.root}/WEB-INF/logs/log.log log4j.appender.D.Append = true log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### Save exception information to separate file ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = ${springmvc.root}/WEB-INF/logs/error.log log4j.appender.D.Append = true log4j.appender.D.Threshold = ERROR log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n 10. 测试 WEB-INF/jsp/index.jsp(测试文件) <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here

欢迎你!!!

11. Controller com.jd.nhc.controller.UserController package com.jd.nhc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/") public class UserController { @RequestMapping("index") public String index() { return "index"; } } 将项目部署在Tomcat中,运行

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 11 人已下载

下载文档