项目地址: https://github.com/wangliang0209/SpringMVCMybatisDemo/
1.首先看pom.xml文件 引入必须的jar
4.0.0
com.walker.maven.demo
web
war
0.0.1-SNAPSHOT
web Maven Webapp
http://maven.apache.org
UTF-8
4.1.5.RELEASE
3.4.1
1.3.0
5.1.40
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis-spring.version}
com.alibaba
druid
1.0.12
mysql
mysql-connector-java
${mysql-connection.version}
org.aspectj
aspectjweaver
1.8.4
log4j
log4j
1.2.17
org.slf4j
slf4j-api
1.7.21
org.slf4j
slf4j-log4j12
1.7.21
runtime
commons-fileupload
commons-fileupload
1.3.2
commons-io
commons-io
2.5
commons-codec
commons-codec
1.10
commons-dbcp
commons-dbcp
1.4
com.alibaba
fastjson
1.1.15
javax
javaee-api
7.0
junit
junit
4.12
test
javax.servlet
jstl
1.2
javax.inject
javax.inject
1
web
org.apache.maven.plugins
maven-war-plugin
2.6
false
2.spring核心配置文件引入
- 在web.xml添加该代码(
注意最好放在前边
)
contextConfigLocation
classpath:spring.xml,classpath:spring-mybatis.xml
这里spring.xml 和spring-mybatis.xml都放在src/main/resources中
- spring.xml 中加载配置文件及扫描注入Service
- spring-mybatis.xml 是集成mybatis的配置文件
3.配置spring mvc
- 首先在web.xml中配置如下
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcher
/
- spring-mvc.xml 配置文件(如果不设置init-param的话默认读取web-inf中dispatcher-servlet.xml文件)
到此,配置文件已经基本结束
4.Mybatis代码生成(由于这部分比较麻烦,可以自己写工具生成默认的文件,包括 model dao mapping)
- model根据数据表结构定义
public class Account {
private Integer id;
private String account;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account == null ? null : account.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
}
- dao代码
import com.wl.model.Account;
public interface AccountMapper {
int deleteByPrimaryKey(Integer id);
int insert(Account record);
int insertSelective(Account record);
Account selectByPrimaryKey(Integer id);
Account selectByAccount(String account);
Account selectByAccPwd(Account account);
int updateByPrimaryKeySelective(Account record);
int updateByPrimaryKey(Account record);
}
- mapping代码
_id, account, password
delete from tb_account
where _id = #{id,jdbcType=INTEGER}
insert into tb_account (_id, account, password
)
values (#{id,jdbcType=INTEGER}, #{account,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
)
insert into tb_account
_id,
account,
password,
#{id,jdbcType=INTEGER},
#{account,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
update tb_account
account = #{account,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
where _id = #{id,jdbcType=INTEGER}
update tb_account
set account = #{account,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR}
where _id = #{id,jdbcType=INTEGER}
5.service代码
- 定义接口
package com.wl.service;
import com.wl.model.Account;
public interface RegService {
Account getAccountByAccount(String account);
Account getAccountByAccPwd(String account, String pwd);
int regAccount(Account account);
}
- 具体实现
@Service("regService")
public class RegServiceImpl implements RegService {
@Autowired
private AccountMapper accountMapper;
public Account getAccountByAccount(String account) {
return accountMapper.selectByAccount(account);
}
public Account getAccountByAccPwd(String account, String pwd) {
Account acc = new Account();
acc.setAccount(account);
acc.setPassword(pwd);
return accountMapper.selectByAccPwd(acc);
}
/**
* @return -2 account exist. > 0 succ.
*/
public int regAccount(Account account) {
int ret = -1;
Account acc = accountMapper.selectByAccount(account.getAccount());
if(acc != null) {
account.setId(acc.getId());
ret = -2;
} else {
accountMapper.insert(account);
return accountMapper.selectByAccount(account.getAccount()).getId();
}
return ret;
}
}
6.到此可以测试了。注意这里没写单元测试代码了,数据库请自行建立
- 在src/test/java 建立同包名的业务,如com.wl.service,创建测试类TestRegService
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring.xml", "classpath:spring-mybatis.xml"})
public class TestRegService {
private static final Logger LOGGER = Logger.getLogger(TestRegService.class);
@Autowired
private RegService regService;
@Test
public void testGetAccountByAccount() {
Account account = regService.getAccountByAccount("test");
if(account == null) {
System.out.println("testGetAccountByAccount >> test not exist.");
} else {
System.out.println("testGetAccountByAccount >> " + JSON.toJSONString(account));
}
}
@Test
public void testGetAccountByAccPwd() {
Account account = regService.getAccountByAccPwd("test", "123456");
if(account == null) {
System.out.println("testGetAccountByAccPwd >> test not exist.");
} else {
System.out.println("testGetAccountByAccPwd >> " + JSON.toJSONString(account));
}
}
@Test
public void testRegAccount() {
Account account = new Account();
account.setAccount("test");
account.setPassword("123456");
int ret = regService.regAccount(account);
System.out.println("testRegAccount >> " + ret);
}
}
单元测试很重要,也可以根据业务方法自行生成的,具体的自己写吧。
7.控制层集成 参考RegController
@Controller
@RequestMapping("/reg")
public class RegController {
@Autowired
private RegService regService;
@RequestMapping(method=RequestMethod.POST)
private @ResponseBody String reg(@RequestParam(value="account") String account,
@RequestParam(value="password") String password) {
if(account == null || "".equals(account)){
return ResponseUtils.generFailedJsonStr(-1, "account is null");
} else if(password == null || "".equals(password)) {
return ResponseUtils.generFailedJsonStr(-1, "pwd is null");
} else {
Account acc = new Account();
acc.setAccount(account);
acc.setPassword(password);
int ret = regService.regAccount(acc);
if(ret > 0) {
JSONObject data = new JSONObject();
data.put("uid", ret);
return ResponseUtils.generSuccJsonStr(data);
} else {
return ResponseUtils.generFailedJsonStr(ret, "reg failed.");
}
}
}
}
到此,集成应该算完成了。
8.遇到的问题
- Validation慢的问题,解决方案就是把Validation中build中web js 相关都勾掉
- mybatis配置文件不提示的问题
然后步入正题,当这个自动提示不管用的时候,如何自己设置让其可以自动提示。
首先,按着ctrl键点击上方的这个头,http://mybatis.org/dtd/mybatis-3-mapper.dtd,这一部分,就会让你下载这个dtd到本机上,找个专用文件夹存好。如果下载不了就说明这个url有问题,那就去往上找把。
下载完成后,打开window–>Preferences–>XML–>XML catalog。自己用搜索框直接搜索xml也可以找到。然后点击add,Location地方找到你下载好的dtd文件,key type用public-Key,key复制xml头上的-//mybatis.org//DTD Mapper 3.0//EN这一部分,然后确定保存,再返回xml文件,你就会发现自动提示能用啦!
这个地方我的猜测是xml头上有个PUBLIC,所以才选择的public-key。还是不知道这个猜测对不对,期待有兴趣的朋友或者大神给解个惑。其他的xml配置,我感觉就是重复这个步骤就ok了吧~
-
cannot change version Dynamic web module to 3.0
1.修改org.eclipse.jdt.core.prefs 中1.5 到1.8(根据自己的jdk设置)
2.修改org.eclipse.wst.common.project.facet.core.xml
-//从2.3改为 3.0
-//改为自己对应的版本
3.注意这里一定要在Navigator中修改,否则可能会没效果。 -
dynamic web module 3.0 requires 1.6
在pom.xml中添加
然后,maven update project 即可。org.apache.maven.plugins maven-compiler-plugin 3.0 1.8