建立springmvc项目,先跑起来,再整合spring和mybatis
1.新建maven工程,安装tomcat
org.springframework
spring-webmvc
4.1.3.RELEASE
web-inf的内容如下
spring_maven
index.jsp
ssm1
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc-servlet.xml
1
ssm1
/
分别为mybatis的配置文件,spring的配置文件,springmvc的配置文件,此处暂时只写springmvc-servlet的内容,剩下两个等说到的时候再填充。springmvc-servlet的内容为:
到此为止,springmvc已经完成,此时可以跑一下springmvc,看是否出现问题
并在controller包中建立一个UserController类,用于测试springmvc
pojo:实体类(用于映射数据库中的字段)
dao:数据访问层(规定接口,用于定义访问数据库中的方法)
service:操作数据访问层,返回处理后的数据给controller层
controller:控制器(功能类似于servlet,位于web层和service层之间)
UserController.java:
package com.bd.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@RequestMapping("/test")
public ModelAndView test1() {
System.out.println("this is userTest");
return null;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
To test
由于maven会自动导入相关联的包,在导入spring-mvc的jar包的时候,spring的基本包都已经集成上去,故此处不需要导入
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring-context.xml
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
CharacterEncodingFilter
/*
spring的集成暂时完成,等下需要将mybatis集成进spring中时,再进一步进行配置
package com.bd.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void test() {
System.out.println("this is UserService");
}
}
将UserController中的内容更改为
package com.bd.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bd.service.UserService;
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/test")
public ModelAndView test1() {
System.out.println("this is userTest");
userService.test();
return null;
}
}
测试结果:
此时spring已经成功集成,现在只剩下最后的mybatis
org.mybatis
mybatis-spring
1.3.1
org.mybatis
mybatis
3.2.8
org.springframework
spring-jdbc
4.1.3.RELEASE
mysql
mysql-connector-java
5.1.9
UserDao.java
package com.bd.dao;
import com.bd.pojo.User;
public interface UserDao {
User getUser(String username);
}
User.java
package com.bd.pojo;
public class User {
int id;
String username;
String password;
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.2 新建一个mapper文件夹,在里面配置UserDao的实现:UserDao.xml,并在main/resources下新建jdbc.properties文件
UserDao.xml
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=123456
此时编译一下,启动servlet,看是否有问题,若问题,此时最基本的ssm框架已经搭建成功,简单调试一下即可
package com.bd.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bd.dao.UserDao;
import com.bd.pojo.User;
@Service
public class UserService {
@Autowired
UserDao userDao;
public void test() {
User user=userDao.getUser("aaa");
System.out.println(user);
}
}
log4j.rootLogger=DEBUG,A1
log4j.logger.org.mybatis=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss,SSS} %m%n
并在service层中需要加强的函数上,添加
@Transactional
jstl
jstl
1.2
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
jsp-api
2.0
provided
com.fasterxml.jackson.core
jackson-databind
2.7.2
com.fasterxml.jackson.core
jackson-core
2.7.2
com.fasterxml.jackson.core
jackson-annotations
2.7.2
log4j
log4j
1.2.17
commons-logging
commons-logging
1.2
junit
junit
test
3.8.2
com.github.pagehelper
pagehelper
5.1.4
org.apache.tomcat.maven
tomcat7-maven-plugin
80
/
UTF-8