版权声明:本文为博主林炳文Evankaka原创文章,转载请注明出处http://blog.csdn.net/evankaka
目录(?)[+]
林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka
摘要:本文主要讲了如何使用Maven来搭建spring+Mybatis+SpringMVC+MySQL的搭建实例,文章写得很详细,有代码有图片,最后也带有运行的效果。
本文工程免费下载
1. 首先创建一个表:
CREATE TABLE `t_user` (
`USER_ID` int(11) NOT NULL AUTO_INCREMENT,
`USER_NAME` char(30) NOT NULL,
`USER_PASSWORD` char(10) NOT NULL,
`USER_EMAIL` char(30) NOT NULL,
PRIMARY KEY (`USER_ID`),
KEY `IDX_NAME` (`USER_NAME`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (1, '林炳文', '1234567@', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (2, 'evan', '123', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (3, 'kaka', 'cadg', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (4, 'simle', 'cscs', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (5, 'arthur', 'csas', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (6, '小德', 'yuh78', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (7, '小小', 'cvff', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (8, '林林之家', 'gvv', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (9, '林炳文Evankaka', 'dfsc', '[email protected]');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD, USER_EMAIL) VALUES (10, 'apple', 'uih6', '[email protected]');
1、Maven工程创建
(1)新建
(2)选择快速框架
(3)输出项目名,包,记得选war(表示web项目,以后可以spingMVC连起来用)
(4)创建好之后
目录如下:
(5)检查下
这三个地方JDK的版本一定要一样!!!!
1、整个工程目录如下:
2、POM文件
4.0.0
com.lin
ssm_project
0.0.1-SNAPSHOT
war
3.2.8.RELEASE
1.6.6
1.2.12
4.10
3.2.1
org.springframework
spring-core
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-web
${spring.version}
junit
junit
${junit.version}
test
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.springframework
spring-test
${spring.version}
test
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.0
mysql
mysql-connector-java
5.1.29
3、java代码-------src/main/java
目录如下:
(1)User.Java
对应数据库中表的字段,放在src/main/java下的包com.lin.domain
package com.lin.domain;
/**
* User映射类
*
* @author linbingwen
* @time 2015.5.15
*/
public class User {
private Integer userId;
private String userName;
private String userPassword;
private String userEmail;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName
+ ", userPassword=" + userPassword + ", userEmail=" + userEmail
+ "]";
}
}
(2)UserDao.java
Dao接口类,用来对应mapper文件。放在src/main/java下的包com.lin.dao,内容如下:
package com.lin.dao;
import com.lin.domain.User;
/**
* 功能概要:User的DAO类
*
* @author linbingwen
* @since 2015年9月28日
*/
public interface UserDao {
/**
*
* @author linbingwen
* @since 2015年9月28日
* @param userId
* @return
*/
public User selectUserById(Integer userId);
}
(2)UserService.java和UserServiceImpl.java
service接口类和实现类,放在src/main/java下的包com.lin.service,内容如下:
UserService.java
package com.lin.service;
import org.springframework.stereotype.Service;
import com.lin.domain.User;
/**
* 功能概要:UserService接口类
*
* @author linbingwen
* @since 2015年9月28日
*/
public interface UserService {
User selectUserById(Integer userId);
}
package com.lin.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lin.dao.UserDao;
import com.lin.domain.User;
/**
* 功能概要:UserService实现类
*
* @author linbingwen
* @since 2015年9月28日
*/
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
public User selectUserById(Integer userId) {
return userDao.selectUserById(userId);
}
}
用来和dao文件对应,放在src/main/java下的com.lin.mapper包下
目录如下:
(1)mybatis配置文件
这里没有什么内容,因为都被放到application.xml中去了,放在src/main/resources下的mybatis文件夹下
mybatis-config.xml内容如下:
放在src/main/resources下的propertiesy文件夹下
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/learning
jdbc_username=root
jdbc_password=christmas258@
(3)Spring配置
这是最重要的:application.xml内容如下
classpath:properties/*.properties
${jdbc_driverClassName}
${jdbc_url}
${jdbc_username}
${jdbc_password}
就放在src/main/resources
log4j.rootLogger=DEBUG,Console,Stdout
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.appender.Stdout = org.apache.log4j.DailyRollingFileAppender
log4j.appender.Stdout.File = E://logs/log.log
log4j.appender.Stdout.Append = true
log4j.appender.Stdout.Threshold = DEBUG
log4j.appender.Stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
上面的配置完好,接下来就是测验成功
整个目录 如下:
(1)测试基类
package com.lin.baseTest;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* 功能概要:
*
* @author linbingwen
* @since 2015年9月28日
*/
//指定bean注入的配置文件
@ContextConfiguration(locations = { "classpath:application.xml" })
//使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class SpringTestCase extends AbstractJUnit4SpringContextTests{
protected Logger logger = LoggerFactory.getLogger(getClass());
}
package com.lin.service;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.lin.baseTest.SpringTestCase;
import com.lin.domain.User;
/**
* 功能概要:UserService单元测试
*
* @author linbingwen
* @since 2015年9月28日
*/
public class UserServiceTest extends SpringTestCase {
@Autowired
private UserService userService;
Logger logger = Logger.getLogger(UserServiceTest.class);
@Test
public void selectUserByIdTest(){
User user = userService.selectUserById(10);
logger.debug("查找结果" + user);
}
}
选中selectUserByIdTest,然后右键如下运行
输出结果:
这里
2015-09-28 15:20:15,129 [main] DEBUG [com.lin.dao.UserDao.selectUserById] - ==> Preparing: SELECT * FROM t_user WHERE USER_ID = ?
2015-09-28 15:20:15,160 [main] DEBUG [com.lin.dao.UserDao.selectUserById] - ==> Parameters: 10(Integer)
2015-09-28 15:20:15,160 [main] DEBUG [org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b64bff9]
2015-09-28 15:20:15,160 [main] DEBUG [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
2015-09-28 15:20:15,160 [main] DEBUG [com.lin.service.UserServiceTest] - 查找结果User [userId=10, userName=apple, userPassword=uih6, [email protected]]
数据库:
4.0.0
com.lin
ssm_project
0.0.1-SNAPSHOT
war
3.2.8.RELEASE
1.6.6
1.2.12
4.10
3.2.1
org.springframework
spring-core
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-web
${spring.version}
junit
junit
${junit.version}
test
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.springframework
spring-test
${spring.version}
test
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.0
mysql
mysql-connector-java
5.1.29
javax
javaee-api
6.0
provided
javax
javaee-web-api
6.0
provided
其实也就增加了下面两个
javax
javaee-api
6.0
provided
javax
javaee-web-api
6.0
provided
(3)配置web,xml
Archetype Created Web Application
index.jsp
contextConfigLocation
classpath:application.xml
webAppRootKey
springmvc.root
SpringEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
SpringEncodingFilter
/*
log4jConfigLocation
classpath:log4j.properties
log4jRefreshInterval
6000
org.springframework.web.util.Log4jConfigListener
org.springframework.web.context.ContextLoaderListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc/spring-mvc.xml
1
dispatcherServlet
/
404
/WEB-INF/errorpage/404.jsp
405
/WEB-INF/errorpage/405.jsp
500
/WEB-INF/errorpage/500.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Hello World!
${user.userId}
${user.userName}
${user.userPassword}
${user.userEmail}
package com.lin.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.lin.domain.User;
import com.lin.service.UserService;
/**
* 功能概要:UserController
*
* @author linbingwen
* @since 2015年9月28日
*/
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/")
public ModelAndView getIndex(){
ModelAndView mav = new ModelAndView("index");
User user = userService.selectUserById(1);
mav.addObject("user", user);
return mav;
}
}