1、MyEclipse+Maven+Tomcat安装与配置
2、MyEclipse创建Maven项目
3、MySQL安装与数据库创建
4、MyEclipse编写后台接口
首先你要有Maven项目,详情见:http://blog.csdn.net/qq_29266921/article/details/53607400
此篇文章,只需要你照猫画虎,别问为什么,先抄下来,下文章会有注释。
开始...
先看一下我的项目目录:
接下来,准备CV了
UserController
/**
* 控制层(目的是返给访问者数据)
* @author Biligle
*
*/
@Controller
public class UserController {
/**
* 注入resource配置
*/
@Resource
private UserService userService;
/**
* 访问入口,接受外界访问
* @param name 接口参数
* @param password 接口参数
* @return 返回map,spring框架会解析成json格式
*/
@ResponseBody
@RequestMapping(value="/showUserInfo",method=RequestMethod.POST)
public Map/*ModelAndView*/ login(
@RequestParam(value = "name", required = true) String name,
@RequestParam(value = "password", required = true) String password){
// ModelAndView mav = new ModelAndView("index");
// User user = userService.selectUserById(2);
// mav.addObject("user", user);
User user = null;
String message = "";
String success = "";
if(StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(password)){
success = "failed";
message = "用户名和密码不能为空";
}else{
user = userService.selectUserByName(name);
if(null == user){
success = "failed";
message = "用户不存在";
}else{
if(password.equals(user.getUserPassword())){
success = "success";
message = "祝贺Biligle登录成功";
}else{
success = "failed";
message = "密码有误";
}
}
}
Map map = new HashMap<>();
map.put("success", success);
map.put("message", message);
return map;
}
}
UserDao
/**
* 查询数据接口
* @author Biligle
*
*/
public interface UserDao {
/**
* 根据ID查询数据库数据
* @param userId 参数名(一定写对,和user2Mapper.xml文件中的userId对应)
* @return
*/
public User selectUserById(Integer userId);
/**
* 根据名字查询数据库
* @param name 参数名(一定写对,和user2Mapper.xml文件中的name对应)
* @return
*/
public User selectUserByName(String name);
}
/**
* 实体类
* @author Biligle
*
*/
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;
}
UserService
/**
* Service接口层(给UserDao分配任务)
* @author Biligle
*
*/
public interface UserService {
User selectUserById(Integer userId);
User selectUserByName(String name);
}
/**
* Service层的实现
* @author Biligle
*
*/
@Service
public class UserServiceImpl implements UserService{
/**
* 注入UserDao
*/
@Autowired
private UserDao userDao;
/**
* 操作UserDao做数据库的操作
*/
public User selectUserById(Integer userId) {
return userDao.selectUserById(userId);
}
/**
* 操作UserDao做数据库的操作
*/
public User selectUserByName(String name) {
return userDao.selectUserByName(name);
}
}
user2Mapper.xml
mybatis-config.xml
jdbc.properties
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc\:mysql\://localhost\:3306/mytest
jdbc_username=root
jdbc_password=chendashan(注意这里要写你数据库的密码)
spring-mvc.xml
application.xml
classpath:properties/*.properties
${jdbc_driverClassName}
${jdbc_url}
${jdbc_username}
${jdbc_password}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
User Info
用户名: ${user.userName}
密码: ${user.userPassword}
maven_springmvc_mybatis_demo
contextConfigLocation
classpath:application.xml
org.springframework.web.context.ContextLoaderListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc/spring-mvc.xml
1
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
dispatcherServlet
/
4.0.0
maven_springmvc_mybatis_demo
maven_springmvc_mybatis_demo
0.0.1-SNAPSHOT
war
maven_springmvc_mybatis_demo
UTF-8
3.2.9.RELEASE
4.10
3.2.8
javax
javaee-api
7.0
provided
javax
javaee-web-api
7.0
provided
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
org.springframework
spring-test
${spring.version}
test
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
5.1.34
com.fasterxml.jackson.core
jackson-core
2.4.4
org.codehaus.jackson
jackson-core-asl
1.9.12
org.codehaus.jackson
jackson-mapper-asl
1.9.12
maven-compiler-plugin
2.3.2
1.7
1.7
maven-war-plugin
2.2
3.1
false
打开你的数据库,运行项目吧。
启动成功之后,用谷歌插件postman,测试后台接口,安装、使用方法,百度知道
测试结果如图:
http://192.168.1.71:8080/maven_springmvc_mybatis_demo/showUserInfo