org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.0
c3p0
c3p0
0.9.1.2
jar
compile
day03_SSM
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
4.项目结构搭建:
编写Account实体类
/**
-
@Author: Ly
-
@Date: 2020-09-29 21:14
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return “Account{” +
“id=” + id +
“, name='” + name + ‘’’ +
“, money=” + money +
‘}’;
}
}
编写业务层接口
/**
-
@Author: Ly
-
@Date: 2020-09-29 21:18
*/
public interface AccountService {
//查询所有账户
public List findAll();
//保存账户信息
public void saveAccount(Account account);
}
[](()整合Spring与SpringMVC
测试Spring框架在web工程中独立运行
第一步:编写 spring 配置文件并导入约束applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:context=“http://www.springframework.org/schema/context”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package=“com.ly”>
<context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>
</context:component-scan>
第二步:使用注解配置业务层
/**
-
@Author: Ly
-
@Date: 2020-09-29 21:19
*/
@Service(“accountService”)
public class AccountServiceImpl implements AccountService {
@Override
public List findAll() {
System.out.println(“业务层:查询所有账户”);
return null;
}
@Override
public void saveAccount(Account account) {
System.out.println(“业务层:保存账户”);
}
}
第三步:测试Spring
/**
-
@Author: Ly
-
@Date: 2020-09-29 21:33
*/
public class TestSpring {
@Test
public void run1(){
//加载配置文件applicationContext.xml
ApplicationContext ac=new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
//获取对象
AccountService as=(AccountService)ac.getBean(“accountService”);
//调用方法
as.findAll();
}
}
测试SpringMVC在web工程中独立运行
第一步:在 web.xml 中配置核心控制器(DispatcherServlet)
Archetype Created Web Application
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
第二步:编写 SpringMVC 的配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package=“com.ly”>
<context:include-filter type=“annotation” expression=“org.springframework.stereotype.Controller” />
</context:component-scan>
<mvc:resources mapping=“/css/**” location=“/css/”/>
<mvc:resources mapping=“/images/**” location=“/images/”/>
<mvc:resources mapping=“/js/**” location=“/js/”/>
mvc:annotation-driven</mvc:annotation-driven>
第三步:编写控制器代码:
/**
-
@Author: Ly
-
@Date: 2020-09-29 21:21
*/
@Controller
@RequestMapping(“/account”)
public class AccountController {
@RequestMapping(“/findAll”)
public String findAll(){
System.out.println(“表现层:查询所有账户信息”);
return “list”;
}
}
第四步:新建jsp页面测试代码
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
成功后的跳转界面
<%@ page contentType=“text/html;charset=UTF-8” language=“java” isELIgnored=“false” %>
查询了所有账户信息
整合Spring和SpringMVC
第一步:在web.xml中配置监听器实现启动服务创建容器
配置 spring 提供的监听器,用于启动服务时加载容器 。该间监听器只能加载 WEB-INF 目录中名称为 applicationContext.xml 的配置文件 ,这样在tomcat服务器启动时,就会加载spring的配置文件了。
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
第二步:在控制器中实现依赖注入
@Autowired
private AccountService accountService;
@RequestMapping(“/findAll”)
public String findAll(Model model){
System.out.println(“表现层:查询所有的信息”);
//调用service方法
accountService.findAll();
return “list”;
}
第三步:再次测试代码
[](()整合Spring与Mybatis
测试MyBatis框架在web工程中独立运行
第一步:编写 AccountDao持久层接口,我们使用代理 dao 的方式来操作持久层,Dao的实现类就是多余的。
/**
-
@Author: Ly
-
@Date: 2020-09-29 21:15
-
账户dao接口
*/
//@Repository
public interface AccountDao {
//查询所有账户
@Select(“select * from account”)
public List findAll();
//保存账户信息
@Insert(“insert into account (name,money) values(#{name},#{money})”)
public void saveAccount(Account account);
}
第二步:编写 SqlMapConfig 配置文件
<?xml version="1.0" encoding="UTF-8"?>第三步:测试运行结果
/**
-
@Author: Ly
-
@Date: 2020-09-30 09:12
*/
public class TestMybatis {
@Test
public void run1() throws Exception {
//加载mybatis配置文件
InputStream in=Resources.getResourceAsStream(“SqlMapConfig.xml”);
//创建SqlSessionFactory对象
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//创建SqlSession对象
SqlSession session=factory.openSession();
//获取到代理对象
AccountDao dao=session.getMapper(AccountDao.class);
//查询所有数据
List list=dao.findAll();
for(Account account:list){
System.out.println(account);
}
//关闭资源
session.close();
in.close();
}
@Test
public void run2() throws Exception {
Account account=new Account();
account.setName(“aas”);
account.setMoney(2222d);
//加载mybatis配置文件
InputStream in=Resources.getResourceAsStream(“SqlMapConfig.xml”);
//创建SqlSessionFactory对象
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//创建SqlSession对象
SqlSession session=factory.openSession();
//获取到代理对象