springmvc+mybatis+maven整合視頻
http://www.java1234.com/a/javaziliao/javaweb/2013/0816/591.html
http://blog.csdn.net/evankaka/article/details/48785513
[置頂]Spring+Mybatis+SpringMVC+Maven+MySql搭建實例
標簽:SpringMybatisspring mvcMaven
2015-09-30 07:5022038人閱讀評論(23)收藏舉報
分類:
林炳文Evankaka原創(chuàng)作品驹溃。轉(zhuǎn)載請注明出處http://blog.csdn.net/evankaka
摘要:本文主要講了如何使用Maven來搭建Spring+Mybatis+SpringMVC+MySQL的搭建實例越走,文章寫得很詳細派昧,有代碼有圖片,最后也帶有運行的效果。
1. 首先創(chuàng)建一個表:
[sql]view plaincopy
CREATETABLE`t_user`?(
`USER_ID`int(11)NOTNULLAUTO_INCREMENT,
`USER_NAME`char(30)NOTNULL,
`USER_PASSWORD`char(10)NOTNULL,
`USER_EMAIL`char(30)NOTNULL,
PRIMARYKEY(`USER_ID`),
KEY`IDX_NAME`?(`USER_NAME`)
)?ENGINE=InnoDB?AUTO_INCREMENT=11DEFAULTCHARSET=utf8
隨便插入一些數(shù)據(jù):
[sql]view plaincopy
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(1,'林炳文','1234567@','ling20081005@126.com');
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(2,'evan','123','fff@126.com');
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(3,'kaka','cadg','fwsfg@126.com');
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(4,'simle','cscs','fsaf@126.com');
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(5,'arthur','csas','fsaff@126.com');
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(6,'小德','yuh78','fdfas@126.com');
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(7,'小小','cvff','fsaf@126.com');
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(8,'林林之家','gvv','lin@126.com');
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(9,'林炳文Evankaka','dfsc','ling2008@126.com');
INSERTINTOt_user?(USER_ID,?USER_NAME,?USER_PASSWORD,?USER_EMAIL)VALUES(10,'apple','uih6','ff@qq.com');
1箱歧、Maven工程創(chuàng)建
(1)新建
(2)選擇快速框架
(3)輸出項目名,包一膨,記得選war(表示web項目呀邢,以后可以spingMVC連起來用)
(4)創(chuàng)建好之后
目錄如下:
(5)檢查下
這三個地方JDK的版本一定要一樣!9薄M斩臁!
1输钩、整個工程目錄如下:
2、POM文件
[html]view plaincopy
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd">
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
對應(yīng)數(shù)據(jù)庫中表的字段买乃,放在src/main/java下的包com.lin.domain
[java]view plaincopy
packagecom.lin.domain;
/**
*?User映射類
*
*?@author?linbingwen
*?@time?2015.5.15
*/
publicclassUser?{
privateInteger?userId;
privateString?userName;
privateString?userPassword;
privateString?userEmail;
publicInteger?getUserId()?{
returnuserId;
}
publicvoidsetUserId(Integer?userId)?{
this.userId?=?userId;
}
publicString?getUserName()?{
returnuserName;
}
publicvoidsetUserName(String?userName)?{
this.userName?=?userName;
}
publicString?getUserPassword()?{
returnuserPassword;
}
publicvoidsetUserPassword(String?userPassword)?{
this.userPassword?=?userPassword;
}
publicString?getUserEmail()?{
returnuserEmail;
}
publicvoidsetUserEmail(String?userEmail)?{
this.userEmail?=?userEmail;
}
@Override
publicString?toString()?{
return"User?[userId="+?userId?+",?userName="+?userName
+",?userPassword="+?userPassword?+",?userEmail="+?userEmail
+"]";
}
}
(2)UserDao.java
Dao接口類,用來對應(yīng)mapper文件钓辆。放在src/main/java下的包com.lin.dao,內(nèi)容如下:
[java]view plaincopy
packagecom.lin.dao;
importcom.lin.domain.User;
/**
*?功能概要:User的DAO類
*
*?@author?linbingwen
*?@since?2015年9月28日
*/
publicinterfaceUserDao?{
/**
*
*?@author?linbingwen
*?@since?2015年9月28日
*?@param?userId
*?@return
*/
publicUser?selectUserById(Integer?userId);
}
(2)UserService.java和UserServiceImpl.java
service接口類和實現(xiàn)類剪验,放在src/main/java下的包com.lin.service,內(nèi)容如下:
UserService.java
[java]view plaincopy
packagecom.lin.service;
importorg.springframework.stereotype.Service;
importcom.lin.domain.User;
/**
*?功能概要:UserService接口類
*
*?@author?linbingwen
*?@since??2015年9月28日
*/
publicinterfaceUserService?{
User?selectUserById(Integer?userId);
}
UserServiceImpl.java
[java]view plaincopy
packagecom.lin.service;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importcom.lin.dao.UserDao;
importcom.lin.domain.User;
/**
*?功能概要:UserService實現(xiàn)類
*
*?@author?linbingwen
*?@since??2015年9月28日
*/
@Service
publicclassUserServiceImplimplementsUserService{
@Autowired
privateUserDao?userDao;
publicUser?selectUserById(Integer?userId)?{
returnuserDao.selectUserById(userId);
}
}
(4)mapper文件
用來和dao文件對應(yīng),放在src/main/java下的com.lin.mapper包下
[html]view plaincopy
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
SELECT?*?FROM?t_user?WHEREUSER_ID=?#{userId}
4前联、資源配置-------src/main/resources
目錄如下:
(1)mybatis配置文件
這里沒有什么內(nèi)容功戚,因為都被放到application.xml中去了,放在src/main/resources下的mybatis文件夾下
mybatis-config.xml內(nèi)容如下:
[html]view plaincopy
"http://mybatis.org/dtd/mybatis-3-config.dtd">
(2)數(shù)據(jù)源配置jdbc.properties
放在src/main/resources下的propertiesy文件夾下
[html]view plaincopy
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/learning
jdbc_username=root
jdbc_password=christmas258@
(3)Spring配置
這是最重要的:application.xml內(nèi)容如下
[html]view plaincopy
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
classpath:properties/*.properties
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-->
${jdbc_driverClassName}
${jdbc_url}
${jdbc_username}
${jdbc_password}
value="com.lin.dao"/>
/>-->
(4)日志打印log4j.properties
就放在src/main/resources
[html]view plaincopy
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)測試基類
[java]view plaincopy
packagecom.lin.baseTest;
importorg.junit.runner.RunWith;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
importorg.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)
publicabstractclassSpringTestCaseextendsAbstractJUnit4SpringContextTests{
protectedLogger?logger?=?LoggerFactory.getLogger(getClass());
}
(2)測試類
[java]view plaincopy
packagecom.lin.service;
importorg.apache.log4j.Logger;
importorg.junit.Test;
importorg.springframework.beans.factory.annotation.Autowired;
importcom.lin.baseTest.SpringTestCase;
importcom.lin.domain.User;
/**
*?功能概要:UserService單元測試
*
*?@author?linbingwen
*?@since??2015年9月28日
*/
publicclassUserServiceTestextendsSpringTestCase?{
@Autowired
privateUserService?userService;
Logger?logger?=?Logger.getLogger(UserServiceTest.class);
@Test
publicvoidselectUserByIdTest(){
User?user?=?userService.selectUserById(10);
logger.debug("查找結(jié)果"+?user);
}
}
選中selectUserByIdTest啸臀,然后右鍵如下運行
輸出結(jié)果:
重要打印的結(jié)果
這里
[plain]view plaincopy
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]?-?查找結(jié)果User?[userId=10,?userName=apple,?userPassword=uih6,?userEmail=ff@qq.com]
數(shù)據(jù)庫:
程序成功運行,并且結(jié)果正確烁落!
到這里配置好spring+mybatis+mysql!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=====================================================
這里開始添加spirngMVC乘粒,并轉(zhuǎn)換maven工程為web項目
接上面的工程接著說伤塌,此時要加spingMVC之前灯萍,工程得再稍微轉(zhuǎn)換下
此時webapp下的結(jié)果還沒有顯示出來,因為此時我們還沒有配置此的項目為web項目
再次進去Properties配置每聪,如下圖所示:如果沒有顯示如下旦棉,那就先把dynamic web module的√給去掉,然后重新再打開 這個
進入药薯,然后輸入如下內(nèi)容
確定OK之后,webapp下面就會自動生成一些文件他爸,如下
(1)修改POM文件果善,增加了一此內(nèi)容诊笤,新的POM文件如下:
[html]view plaincopy
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd">
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
其實也就增加了下面兩個
[html]view plaincopy
javax
javaee-api
6.0
provided
javax
javaee-web-api
6.0
provided
(2) 在src/main/resource中添加springmvc文件夾,然后添加文件spring-mvc.xml巾陕,內(nèi)容如下:
[html]view plaincopy
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
p:prefix="/WEB-INF/view/"p:suffix=".jsp"/>
(3)配置web,xml
目錄如下 :
這里是關(guān)鍵讨跟,web.xml是用來起動sping纪他、spingMVC
放在src/main/webapp/WEB-INF下。內(nèi)容如下:
[html]view plaincopy
xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">
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
(4)添加index.jsp
在src/main/webapp/WEB-INF下新建一個文件夾view,添加一個index.jsp晾匠,內(nèi)容如下:
[html]view plaincopy
<%@?pagelanguage="java"contentType="text/html;?charset=UTF-8"pageEncoding="UTF-8"%>
Hello?World!
${user.userId}
${user.userName}
${user.userPassword}
${user.userEmail}
(5)最后就是controller的書寫了
在src/main/java下新建一個包com.lin.controller.然后新建一個類UserController.java,其內(nèi)容如下:
[java]view plaincopy
packagecom.lin.controller;
importjavax.annotation.Resource;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.servlet.ModelAndView;
importcom.lin.domain.User;
importcom.lin.service.UserService;
/**
*?功能概要:UserController
*
*?@author?linbingwen
*?@since??2015年9月28日
*/
@Controller
publicclassUserController?{
@Resource
privateUserService?userService;
@RequestMapping("/")
publicModelAndView?getIndex(){
ModelAndView?mav?=newModelAndView("index");
User?user?=?userService.selectUserById(1);
mav.addObject("user",?user);
returnmav;
}
}
(6)最后的運行茶袒!
最后,就是成果了凉馆,已經(jīng)全部都配置好了薪寓。我們可以開始以web工程運行了!
然后一路next
接下來控制臺就會打印日志出來了澜共!如下
打印瀏覽器向叉,輸入網(wǎng)址:http://localhost:8088/ssm_project/