1.SSM介绍
Spring+SpringMVC+MyBatis
2. 集成ssmmaven
2.1 创建项目
- 打开idea,选择Create New Project-->Maven-->maven-archeyupe-webapp-->Next
- GroupId,分组Id,我这里取cn.wangningboArtifact,项目名字,我这里取SSMMaven
- Next-->...-->Finish
- idea右下角选择自动导包
2.2 创建maven的结构
- 在main目录下创建java和resources目录
- 在src下创建测试目录test,在test下创建java和resources目录
- 给目录上色
2.3 配置tomcat
选择Deployment-->Artifact-->war exploded
2.4 测试
启动tomcat测试能否访问index.jsp
2.5 配置pom.xml
- 修改pom.xml中properties这里为1.8
UTF-8
1.8
1.8
- 在properties中配置版本
UTF-8
1.8
1.8
UTF-8
4.1.2.RELEASE
3.2.1
1.7.2
1.2.17
2.5.0
- 下载包
junit
junit
4.11
test
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-tx
${spring.version}
commons-logging
commons-logging
1.1.1
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
aopalliance
aopalliance
1.0
org.aspectj
aspectjweaver
1.6.8
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.2
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
commons-dbcp
commons-dbcp
1.2.2
commons-pool
commons-pool
1.5.3
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.0
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
test
mysql
mysql-connector-java
5.1.46
jstl
jstl
1.2
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet.jsp
jsp-api
2.2
provided
- 最终pom.xml的样子
4.0.0
cn.wangningbo
SSMMaven
1.0-SNAPSHOT
war
SSMMaven Maven Webapp
http://www.example.com
UTF-8
1.8
1.8
UTF-8
4.1.2.RELEASE
3.2.1
1.7.2
1.2.17
2.5.0
junit
junit
4.11
test
org.springframework
spring-aop
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-tx
${spring.version}
commons-logging
commons-logging
1.1.1
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
aopalliance
aopalliance
1.0
org.aspectj
aspectjweaver
1.6.8
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.2
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
commons-dbcp
commons-dbcp
1.2.2
commons-pool
commons-pool
1.5.3
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.0
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
test
mysql
mysql-connector-java
5.1.46
jstl
jstl
1.2
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet.jsp
jsp-api
2.2
provided
SSMMaven
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
2.6配置web.xml
- 粘贴到
标签里
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext-mvc.xml
1
springmvc
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
- 最终web.xml的样子
Archetype Created Web Application
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext-mvc.xml
1
springmvc
/
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
2.7 配置applicationContext-mvc.xml
- 在main下的resources目录下新建applicationContext-mvc.xml,粘贴进去
2.8 配置applicationContext.xml
- 在main下的resources目录下新建applicationContext.xml,粘贴进去
cn.wangningbo.domain
Cn.wangningbo.query
2.9 配置db.properties
在main下的resources目录新建一个db.properties文件,粘贴进去
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///wangningbo?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=apy06942
2.10 配置log4j.properties
log4j.rootLogger=ERROR, stdout
#log4j.rootLogger=NONE
log4j.logger.cn.wangningbo=TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
2.11 集成SSM测试
- 在domain包下创建Employee类
- cn.wangningbo.domain.Employee
public class Employee {
private Long id;
private String name;
//提供get和set方法
}
- 创建数据库
- 创建数据库,名字和db配置文件名字相同叫wangningbo
- 创建表,名字叫employee
- 写入一个数据,用于测试
- 在mapper包下创建EmployeeMapper接口
public interface EmployeeMapper {
List loadAll();
}
- 在main下的resoures新建一个目录,名字叫cn/wangningbo/mapper,在这个目录下新建一个EmployeeMapper.xml,粘贴进去
- 注意:这里的namespace拷贝的是mapper包下的接口的完全限定名
- 在service包下新建接口IEmployeeService
public interface IEmployeeService {
List getAll();
}
- 在service包下新建一个impl包,在impl包下新建一个实现类EmployeeServiceImpl并实现IEmployeeService接口.记得在类上面打Service注解
@Service
public class EmployeeServiceImpl implements IEmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public List getAll() {
return employeeMapper.loadAll();
}
}
- 在src下的test下的java中创建测试基类BaseTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BaseTest {
}
- 再创建个接口的测试类,继承于BaseTest类
public class IEmployeeServiceTest extends BaseTest {
@Autowired
private IEmployeeService employeeService;
@Test
public void getAll() {
employeeService.getAll().forEach(e -> System.out.println(e));
}
}
2.12完善web层与测试
- 创建web包,在web包下创建controller包,在controller包下创建EmployeeController类
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private IEmployeeService employeeService;
@ResponseBody
@RequestMapping("/list")
public List list() {
return employeeService.getAll();
}
}
- 测试
- 启动tomcat,浏览器地址栏访问http://localhost/employee/list,查看是否能拿到数据