开发环境介绍
IDE: MyEclipse 10.7
jdk: 1.6
maven:apache-maven-3.2.5
基本环境只要上面这些就行.
Maven的安装
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,
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) VALUES (1, 'luoguohui', '123456');
INSERT INTO t_user (USER_ID, USER_NAME, USER_PASSWORD) VALUES (2, 'zhangsan', '123456');
2、Maven工程创建,选择快速框架
3 输出项目名,包(Packaging,如果只是普通的项目,选jar就好了,如果是web项目就选war,这里是web项目,所以选择war)
4、创建好的目录如下:
5 、添加包的依赖,编辑pom.xml文件添加如下依赖:
4.0.0
com.gp6
MyMavenProject
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
6、配置文件:
6、1 创建 mybatis包下添加mybatis-config.xml文件(mybatis配置文件):
6. 2、 创建 properties包下添加jdbc.properties文件(数据源配置文件):
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/study
jdbc_username=root
jdbc_password=123456
6. 3、 创建 mapper包下userMapper.xml配置文件(表映射文件):
一定要注意这一行的值是否正确
6. 4、 创建 spring配置文件application.xml:
classpath:properties/*.properties
${jdbc_driverClassName}
${jdbc_url}
${jdbc_username}
${jdbc_password}
7 接口和类的配置:
7.1 、com.gp6.entity 下添加User.Java文件:
package com.gp6.entity;
public class User {
private Integer userId;
private String userName;
private String userPassword;
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;
}
}
7.2、com.gp6.mapper下添加Usermapper.java文件:
package com.gp6.mapper;
import com.gp6.entity.User;
public interface UserMapper {
/**
* @param userId
* @return User
*/
public User selectUserById(Integer userId);
}
7.3、com.gp6.service下添加UserService.java接口和UserServiceImpl实现类:
- UserService.java
package com.gp6.service;
import com.gp6.entity.User;
public interface UserService {
public User selectUserById(Integer userId);
}
- UserServiceImpl.java
package com.gp6.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gp6.entity.User;
import com.gp6.mapper.UserMapper;
import com.gp6.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User selectUserById(Integer userId) {
return userMapper.selectUserById(userId);
}
}
8 单元测试
8.1 BaseTest
package com.gp6.baseTest;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//指定bean注入的配置文件
@ContextConfiguration(locations = { "classpath:application.xml" })
//使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner
@RunWith(SpringJUnit4ClassRunner.class)
public class BaseTest extends AbstractJUnit4SpringContextTests{
}
8.2 UserServiceTest
package com.gp6.serviceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.Test;
import com.gp6.baseTest.BaseTest;
import com.gp6.entity.User;
import com.gp6.service.UserService;
public class UserServiceTest extends BaseTest {
@Autowired
private UserService userService;
@Test
public void selectUserByIdTest(){
User user = userService.selectUserById(1);
System.out.println(user.getUserName() + ":" + user.getUserPassword());
}
}
8.3 运行单元测试,UserServiceTest右键Run As –>Junit Test,运行结果:
8.4 添加日志配置
- log4j.properties
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://WorkSpace/Study/logs/MyMavenProject/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
到这里配置好spring+mybatis+mysql!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
这里开始添加spirngMVC,并转换maven工程为web项目
9 、转换成web项目:
-
Eclipse
MyEclipse
将红框里面的勾去掉,确定(OK),然后重新打开刚刚那个页面,把Dynamic web Module勾上,就会看到红框的内容,点击:
配置如下,然后点击OK
至此,项目已转为WEB项目!
10 配置springmvc
10.1、pom.xml文件添加依赖,修改后配置如下:
4.0.0
com.gp6
MyMavenProject
0.0.1-SNAPSHOT
war
3.2.8.RELEASE
1.6.6
1.2.12
4.11
3.2.1
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
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
10.2、在src/main/resource中添加springmvc文件夹,然后添加文件spring-mvc.xml,内容如下:
10.3、配置web.xml
dispatcherServlet
/*
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
/*
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
10.4、添加index.jsp,在src/main/webapp/WEB-INF下新建一个文件夹jsp,添加一个index.jsp,内容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Hello World!
用户名: ${user.userName}
密码:${user.userPassword}
10.5、写controller
在src/main/java下新建一个包com.gp6.controller.然后新建一个类UserController.java,其内容如下
package com.gp6.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.gp6.entity.User;
import com.gp6.service.UserService;
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/")
public ModelAndView getIndex(){
ModelAndView modelAndView = new ModelAndView("index");
User user = userService.selectUserById(1);
modelAndView.addObject("user", user);
return modelAndView;
}
}