1 2 3 4 5 6 7 8 9 |
|
"http://mybatis.org/dtd/mybatis-3-config.dtd">
jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/web_test
jdbc_username=root
jdbc_password=Aa111111
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
SELECT * FROM book_user WHERE USER_ID = #{userId}
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"> class="org.springframework.jdbc.datasource.DriverManagerDataSource"> value="com.hlx.dao" />
package com.hlx.domain;
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;
}
}
package com.hlx.dao;
import com.hlx.domain.User;
public interface UserDao {
/**
*
* @param userId
* @return
*/
public User selectUserById(Integer userId);
}
package com.hlx.service;
import com.hlx.domain.User;
public interface UserService {
public User selectUserById(Integer userId);
}
package com.hlx.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hlx.dao.UserDao;
import com.hlx.domain.User;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public User selectUserById(Integer userId) {
return userDao.selectUserById(userId);
}
}
package com.hlx.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 SpringTestCase extends AbstractJUnit4SpringContextTests {
}
package com.hlx.service;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.hlx.baseTest.SpringTestCase;
import com.hlx.domain.User;
public class UserServiceTest extends SpringTestCase {
@Autowired
private UserService userService;
@Test
public void selectUserByIdTest(){
User user = userService.selectUserById(10000003);
System.out.println(user.getUserName() + ":" + user.getUserPassword());
}
}
如果上面webapp为空的,说明这个项目还不是web项目:
接下来打开如下页面。将红框里面的勾去掉,确定(OK):
然后重新打开刚刚那个页面,把Dynamic web Module勾上(显示需要更高的java版本,但调整java版本后依旧显示更高版本;所以在这里就将Dynamic Web Module版本调低),就会看到红框的内容,点击:
然后配置如下:
那么webapp下就会生成这些东西:
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"/>
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
用户名: ${user.userName}
密码:${user.userPassword}
package com.hlx.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.hlx.domain.User;
import com.hlx.service.UserService;
@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;
}
}
配置后发现工程没有部署上,所以修改相关路径
13.备注说明
13.1主要参考文档
http://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=2651477246&idx=2&sn=f30d754635ec5a3f589574707c12c263&scene=21#wechat_redirect
13.2用注解Resource时出错,本人改变了javaSE版本
13.3可能会遇到的错
之前有个兄弟按照我的配置,但是发现pom.xml文件报如下错误:
Cannot detect Web Project version. Please specify version of Web Project through
解决方案是,如下的三个地方jdk版本需要保持一致:
1、项目右键->属性->Java Compiler:
2、项目右键->属性->Project Facets:
3、如果pom.xml配置了如下的插件的话:\
这里也要跟上面两个保持一致:
https://blog.csdn.net/zackwang2006/article/details/78854675?utm_source=blogxgwz0