SSM
需求和步骤分析
使用 ssm 框架完成对 account 表的增删改查操作
步骤分析
11. 准备数据库和表记录
22. 创建web项目
33. 编写mybatis在ssm环境中可以单独使用
44. 编写spring在ssm环境中可以单独使用
55. spring整合mybatis
66. 编写springMVC在ssm环境中可以单独使用
77. spring整合springMVC
环境搭建
创建 Web 项目,导入依赖
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.soulboy</groupId>
8 <artifactId>ssm</artifactId>
9 <version>1.0-SNAPSHOT</version>
10
11 <!--指定编码及版本-->
12 <properties>
13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14 <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
15 <java.version>1.11</java.version>
16 <maven.compiler.source>1.11</maven.compiler.source>
17 <maven.compiler.target>1.11</maven.compiler.target>
18 </properties>
19
20 <!-- 打包方式 -->
21 <packaging>war</packaging>
22
23</project>
环境搭建
- 准备数据库和表记录
1CREATE DATABASE spring_db
2use `spring_db`
3
4CREATE TABLE `account` (
5`id` INT(11) NOT NULL AUTO_INCREMENT,
6`name` VARCHAR(32) DEFAULT NULL,
7`money` DOUBLE DEFAULT NULL,
8PRIMARY KEY (`id`)
9);
10
11INSERT INTO `account`(`id`,`name`,`money`) VALUES (5,'tom',1000),(6,'jerry',1000)
编写 MyBatis 在 ssm 环境中可以单独使用
需求:基于 MyBatis 先来实现对 account 表的查询
- 相关坐标
1<!--mybatis坐标-->
2 <dependency>
3 <groupId>mysql</groupId>
4 <artifactId>mysql-connector-java</artifactId>
5 <version>5.1.47</version>
6 </dependency>
7 <dependency>
8 <groupId>com.alibaba</groupId>
9 <artifactId>druid</artifactId>
10 <version>1.1.15</version>
11 </dependency>
12 <dependency>
13 <groupId>org.mybatis</groupId>
14 <artifactId>mybatis</artifactId>
15 <version>3.5.1</version>
16 </dependency>
17 <dependency>
18 <groupId>junit</groupId>
19 <artifactId>junit</artifactId>
20 <version>4.12</version>
21 </dependency>
- Account 实体
1package com.soulboy.domain;
2
3public class Account {
4 private Integer id;
5 private String name;
6 private Double money;
7
8 public Account(Integer id, String name, Double money) {
9 this.id = id;
10 this.name = name;
11 this.money = money;
12 }
13
14 public Integer getId() {
15 return id;
16 }
17
18 public void setId(Integer id) {
19 this.id = id;
20 }
21
22 public String getName() {
23 return name;
24 }
25
26 public void setName(String name) {
27 this.name = name;
28 }
29
30 public Double getMoney() {
31 return money;
32 }
33
34 public void setMoney(Double money) {
35 this.money = money;
36 }
37
38 @Override
39 public String toString() {
40 return "Account{" +
41 "id=" + id +
42 ", name='" + name + '\'' +
43 ", money=" + money +
44 '}';
45 }
46}
3.AccountDao 接口
1package com.soulboy.dao;
2
3import com.soulboy.domain.Account;
4
5import java.util.List;
6
7public interface AccountDao {
8 /*
9 查询所有
10 */
11 public List<Account> findAll();
12}
- AccountDao.xml 映射 (路径 resources: com/soulboy/dao/AccountDao.xml)
1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5<mapper namespace="com.soulboy.dao.AccountDao">
6 <select id="findAll" resultType="Account">
7 select * from account
8 </select>
9</mapper>
- MyBatis 核心配置文件
src/main/resources/SqlMapConfig.xml
1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE configuration
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5
6<configuration>
7 <!-- 加载properties文件 -->
8 <properties resource="jdbc.properties"></properties>
9 <!-- 设置别名 -->
10 <typeAliases>
11 <!-- 方式一: 给单个实体起别名 -->
12 <!-- <typeAlias type="com.soulboy.domain.User" alias="user"></typeAlias> -->
13 <!-- 方式二: 批量起别名 -->
14 <package name="com.soulboy.domain"/>
15 </typeAliases>
16 <!-- 运行环境 -->
17 <environments default="development">
18 <environment id="development">
19 <!-- 当前的事务交由JDBC进行管理 -->
20 <transactionManager type="JDBC"></transactionManager>
21 <!-- 数据源信息 POOLED:使用Mybatis的连接池 UNPOOLED:不使用连接池,每一次请求都会创建连接-->
22 <dataSource type="POOLED">
23 <property name="driver" value="${jdbc.driver}"/>
24 <property name="url" value="${jdbc.url}"/>
25 <property name="username" value="${jdbc.username}"/>
26 <property name="password" value="${jdbc.password}"/>
27 </dataSource>
28 </environment>
29 </environments>
30 <!-- 引入映射配置文件 -->
31 <mappers>
32 <!-- 使用该方式: XML文件的全路径-->
33 <!--<mapper resource="com.soulboy/mapper/UserMapper.xml"></mapper>-->
34
35 <!-- 使用该方式: 接口和映射文件需要同包同名 -->
36 <!--<mapper class="com.soulboy.mapper.UserMapper"></mapper>-->
37
38 <!-- 批量加载映射: Mapper接口的包路径 -->
39 <package name="com.soulboy.dao"/>
40 </mappers>
41</configuration>
jdbc.properties
1jdbc.driver=com.mysql.jdbc.Driver
2jdbc.url=jdbc:mysql://localhost:50000/spring_db?useSSL=false
3jdbc.username=root
4jdbc.password=123456
- 测试代码
com/soulboy/test/MybatisTest.java
1package com.soulboy.test;
2
3import com.soulboy.dao.AccountDao;
4import com.soulboy.domain.Account;
5import org.apache.ibatis.io.Resources;
6import org.apache.ibatis.session.SqlSession;
7import org.apache.ibatis.session.SqlSessionFactory;
8import org.apache.ibatis.session.SqlSessionFactoryBuilder;
9import org.junit.Test;
10
11import java.io.IOException;
12import java.io.InputStream;
13import java.util.List;
14
15public class MybatisTest {
16 @Test
17 public void testMybatis() throws IOException {
18 InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
19 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
20 SqlSession sqlSession = sqlSessionFactory.openSession();
21 AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
22 List<Account> accountList = accountDao.findAll();
23 for (Account account : accountList) {
24 System.out.println(account);
25 }
26 sqlSession.close();
27 }
28}
编写 Spring 在 ssm 环境中可以单独使用
1.相关坐标
1<!-- spring依赖 -->
2 <dependency>
3 <groupId>org.springframework</groupId>
4 <artifactId>spring-context</artifactId>
5 <version>5.1.5.RELEASE</version>
6 </dependency>
7 <!-- aspectj的织入(切点表达式需要用到该jar包) -->
8 <dependency>
9 <groupId>org.aspectj</groupId>
10 <artifactId>aspectjweaver</artifactId>
11 <version>1.8.14</version>
12 </dependency>
13 <!--此处需要注意的是,spring5 及以上版本要求 junit 的版本必须是 4.12 及以上-->
14 <dependency>
15 <groupId>junit</groupId>
16 <artifactId>junit</artifactId>
17 <version>4.12</version>
18 </dependency>
19 <dependency>
20 <groupId>org.springframework</groupId>
21 <artifactId>spring-test</artifactId>
22 <version>5.1.5.RELEASE</version>
23 </dependency>
24 <dependency>
25 <groupId>org.springframework</groupId>
26 <artifactId>spring-jdbc</artifactId>
27 <version>5.1.5.RELEASE</version>
28 </dependency>
29 <dependency>
30 <groupId>org.springframework</groupId>
31 <artifactId>spring-tx</artifactId>
32 <version>5.1.5.RELEASE</version>
33 </dependency>
- AccountService 接口
1package com.soulboy.service;
2
3import com.soulboy.domain.Account;
4
5import java.util.List;
6
7public interface AccountService {
8 public List<Account> findAll();
9}
- AccountServiceImpl 实现
1package com.soulboy.service.impl;
2
3import com.soulboy.dao.AccountDao;
4import com.soulboy.domain.Account;
5import com.soulboy.service.AccountService;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.stereotype.Service;
8
9import java.util.List;
10
11@Service
12public class AccountServiceImpl implements AccountService {
13 @Autowired
14 private AccountDao accountDao;
15
16 @Override
17 public List<Account> findAll() {
18 //System.out.println("findAll执行了……");
19 List<Account> list = accountDao.findAll();
20 return list;
21 }
22}
- Spring 核心配置文件
applicationContext.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:aop="http://www.springframework.org/schema/aop"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context.xsd
12 http://www.springframework.org/schema/aop
13 http://www.springframework.org/schema/aop/spring-aop.xsd
14 http://www.springframework.org/schema/tx
15 http://www.springframework.org/schema/tx/spring-tx.xsd">
16
17 <!--注解扫描-->
18 <context:component-scan base-package="com.soulboy"></context:component-scan>
19
20</beans>
- 测试代码
1package com.soulboy.test.spring;
2
3import com.soulboy.service.AccountService;
4import org.junit.Test;
5import org.junit.runner.RunWith;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.test.context.ContextConfiguration;
8import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9
10@RunWith(SpringJUnit4ClassRunner.class)
11@ContextConfiguration("classpath:applicationContext.xml")
12public class SpringTest {
13 @Autowired
14 private AccountService accountService;
15
16 @Test
17 public void testSpring() {
18 accountService.findAll();
19 }
20}
Spring 整合 MyBatis
- 整合思想
将 MyBatis 接口代理对象的创建权交给 Spring 管理,我们就可以把 dao 的代理对象注入到 service 中,此时也就完成了 Spring 与 MyBatis 的整合了。 - 导入整合包
1<!--mybatis整合spring坐标-->
2 <dependency>
3 <groupId>org.mybatis</groupId>
4 <artifactId>mybatis-spring</artifactId>
5 <version>1.3.1</version>
6 </dependency>
- Spring 配置文件管理 MyBatis
注意:此时可以将 MyBatis 主配置文件删除。
src/main/resources/applicationContext.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:aop="http://www.springframework.org/schema/aop"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xsi:schemaLocation="
8 http://www.springframework.org/schema/beans
9 http://www.springframework.org/schema/beans/spring-beans.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context.xsd
12 http://www.springframework.org/schema/aop
13 http://www.springframework.org/schema/aop/spring-aop.xsd
14 http://www.springframework.org/schema/tx
15 http://www.springframework.org/schema/tx/spring-tx.xsd">
16
17 <!--注解扫描-->
18 <context:component-scan base-package="com.soulboy"></context:component-scan>
19
20 <!--spring整合mybatis-->
21 <context:property-placeholder location="classpath:jdbc.properties"/>
22 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
23 <property name="driverClassName" value="${jdbc.driver}"/>
24 <property name="url" value="${jdbc.url}"/>
25 <property name="username" value="${jdbc.username}"/>
26 <property name="password" value="${jdbc.password}"/>
27 </bean>
28
29 <!--SqlSessionFactory创建交给spring的IOC容器-->
30 <bean id="sqlSessionFactory"
31 class="org.mybatis.spring.SqlSessionFactoryBean">
32 <!--数据库环境配置-->
33 <property name="dataSource" ref="dataSource"/>
34 <!--类型别名配置-->
35 <property name="typeAliasesPackage" value="com.soulboy.domain"/>
36 <!--如果要引入mybatis主配置文件,可以通过如下配置-->
37 <!--<property name="configLocation"
38 value="classpath:SqlMapConfig.xml"/>-->
39 </bean>
40
41 <!--映射接口扫描配置,由spring创建代理对象,交给IOC容器-->
42 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
43 <property name="basePackage" value="com.soulboy.dao"/>
44 </bean>
45</beans>
- 修改 AccountServiceImpl
1package com.soulboy.service.impl;
2
3import com.soulboy.dao.AccountDao;
4import com.soulboy.domain.Account;
5import com.soulboy.service.AccountService;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.stereotype.Service;
8
9import java.util.List;
10
11@Service
12public class AccountServiceImpl implements AccountService {
13 @Autowired
14 private AccountDao accountDao;
15
16 @Override
17 public List<Account> findAll() {
18 //System.out.println("findAll执行了……");
19 List<Account> list = accountDao.findAll();
20 return list;
21 }
22}
- 测试
1package com.soulboy.test.spring;
2
3import com.soulboy.domain.Account;
4import com.soulboy.service.AccountService;
5import org.junit.Test;
6import org.junit.runner.RunWith;
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.test.context.ContextConfiguration;
9import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10
11import java.util.List;
12
13@RunWith(SpringJUnit4ClassRunner.class)
14@ContextConfiguration("classpath:applicationContext.xml")
15public class SpringTest {
16 @Autowired
17 private AccountService accountService;
18
19 @Test
20 public void testSpring() {
21 List<Account> accountList = accountService.findAll();
22 for (Account account : accountList) {
23 System.out.println(account);
24 }
25 }
26}
编写 SpringMVC 在 ssm 环境中可以单独使用
需求:访问到 controller 里面的方法查询所有账户,并跳转到 list.jsp 页面进行列表展示
- 相关坐标
1<!--springMVC坐标-->
2 <dependency>
3 <groupId>org.springframework</groupId>
4 <artifactId>spring-webmvc</artifactId>
5 <version>5.1.5.RELEASE</version>
6 </dependency>
7 <!--servlet坐标-->
8 <dependency>
9 <groupId>javax.servlet</groupId>
10 <artifactId>javax.servlet-api</artifactId>
11 <version>3.1.0</version>
12 <scope>provided</scope>
13 </dependency>
14 <!--jsp坐标-->
15 <dependency>
16 <groupId>javax.servlet.jsp</groupId>
17 <artifactId>jsp-api</artifactId>
18 <version>2.2</version>
19 <scope>provided</scope>
20 </dependency>
21 <dependency>
22 <groupId>jstl</groupId>
23 <artifactId>jstl</artifactId>
24 <version>1.2</version>
25 </dependency>
- 导入页面资源
- 前端控制器 DispathcerServlet
src/main/webapp/WEB-INF/web.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
5 version="4.0">
6 <!--前端控制器-->
7 <servlet>
8 <servlet-name>DispatcherServlet</servlet-name>
9 <servletclass>org.springframework.web.servlet.DispatcherServlet</servlet-class>
10 <init-param>
11 <param-name>contextConfigLocation</param-name>
12 <param-value>classpath:spring-mvc.xml</param-value>
13 </init-param>
14 <load-on-startup>2</load-on-startup>
15 </servlet>
16 <servlet-mapping>
17 <servlet-name>DispatcherServlet</servlet-name>
18 <url-pattern>/</url-pattern>
19 </servlet-mapping>
20 <!--post中文处理-->
21 <filter>
22 <filter-name>CharacterEncodingFilter</filter-name>
23 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
24 <init-param>
25 <param-name>encoding</param-name>
26 <param-value>UTF-8</param-value>
27 </init-param>
28 </filter>
29 <filter-mapping>
30 <filter-name>CharacterEncodingFilter</filter-name>
31 <url-pattern>/*</url-pattern>
32 </filter-mapping>
33
34</web-app>
- AccountController
AccountController
1package com.soulboy.controller;
2
3import com.soulboy.domain.Account;
4import org.springframework.stereotype.Controller;
5import org.springframework.ui.Model;
6import org.springframework.web.bind.annotation.RequestMapping;
7
8import java.util.ArrayList;
9import java.util.List;
10
11@Controller
12@RequestMapping("/account")
13public class AccountController {
14 @RequestMapping("/findAll")
15 public String findAll(Model model) {
16 List<Account> list = new ArrayList<>();
17 list.add(new Account(1,"张三",1000d));
18 list.add(new Account(2,"李四",1000d));
19 model.addAttribute("list", list);
20 return "list";
21 }
22}
JSP 页面
src/main/webapp/list.jsp
1<c:forEach items="${list}" var="account">
2
3 <tr>
4 <td>
5 <input type="checkbox" name="ids" value="${account.id}">
6 </td>
7 <td>${account.id}</td>
8 <td>${account.name}</td>
9 <td>${account.money}</td>
10 <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/account/findById?id=${account.id}">修改</a> <a class="btn btn-default btn-sm" href="">删除</a></td>
11 </tr>
12 </c:forEach>
- SpringMVC 核心配置文件
src/main/resources/spring-mvc.xml
1<beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:mvc="http://www.springframework.org/schema/mvc"
3 xmlns:context="http://www.springframework.org/schema/context"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6http://www.springframework.org/schema/beans/spring-beans.xsd
7http://www.springframework.org/schema/mvc
8http://www.springframework.org/schema/mvc/spring-mvc.xsd
9http://www.springframework.org/schema/context
10http://www.springframework.org/schema/context/spring-context.xsd">
11 <!--组件扫描-->
12 <context:component-scan base-package="com.soulboy.controller"/>
13 <!--mvc注解增强-->
14 <mvc:annotation-driven/>
15 <!--视图解析器-->
16 <bean
17 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
18 <property name="prefix" value="/"/>
19 <property name="suffix" value=".jsp"/>
20 </bean>
21 <!--实现静态资源映射-->
22 <mvc:default-servlet-handler/>
23</beans>
- 配置 Tomcat 并启动并测试
http://localhost:8080/ssm/account/findAll
Spring 整合 SpringMVC
整合思想
Spring 和 SpringMVC 其实根本就不用整合,本来就是一家。
但是 Spring 和 Web 容器需要整合,让 Web 容器启动的时候自动加载 Spring 配置文件,Web 容器销毁的时候 Spring 的 IoC 容器也销毁。
Spring 和 Web 容器整合
ContextLoaderListener 加载【掌握】
可以使用 spring-web 包中的 ContextLoaderListener 监听器,可以监听 servletContext 容器的创建和销毁,来同时创建或销毁 IoC 容器。
src/main/webapp/WEB-INF/web.xml
1<!--spring 与 web容器整合-->
2 <listener>
3 <listener-class>
4 org.springframework.web.context.ContextLoaderListener
5 </listener-class>
6 </listener>
7 <context-param>
8 <param-name>contextConfigLocation</param-name>
9 <param-value>classpath:applicationContext.xml</param-value>
10 </context-param>
修改 AccountController(注入 service:controller 调用 service,service 调用 dao)
AccountController
1package com.soulboy.controller;
2
3import com.soulboy.domain.Account;
4import com.soulboy.service.AccountService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.stereotype.Controller;
7import org.springframework.ui.Model;
8import org.springframework.web.bind.annotation.RequestMapping;
9
10import java.util.List;
11
12@Controller
13@RequestMapping("/account")
14public class AccountController {
15 @Autowired
16 private AccountService accountService;
17
18 @RequestMapping("/findAll")
19 public String findAll(Model model) {
20 List<Account> list = accountService.findAll();
21 model.addAttribute("list", list);
22 return "list";
23 }
24}
配置 Tomcat 并启动并测试
http://localhost:8080/ssm/account/findAll
Spring 配置声明式事务(XML 或注解)
- Spring 配置文件加入声明式事务
applicationContext.xml
1<!--事务管理器-->
2 <bean id="transactionManager"
3 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
4 <property name="dataSource" ref="dataSource"></property>
5 </bean>
6 <!--开启事务注解支持-->
7 <tx:annotation-driven/>
- service 层添加 @Transactional 注解
AccountServiceImpl
1package com.soulboy.service.impl;
2
3import com.soulboy.dao.AccountDao;
4import com.soulboy.domain.Account;
5import com.soulboy.service.AccountService;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.stereotype.Service;
8import org.springframework.transaction.annotation.Transactional;
9
10import java.util.List;
11
12@Service
13@Transactional
14public class AccountServiceImpl implements AccountService {
15 @Autowired
16 private AccountDao accountDao;
17
18 @Override
19 public List<Account> findAll() {
20 //System.out.println("findAll执行了……");
21 List<Account> list = accountDao.findAll();
22 return list;
23 }
24}
- add.jsp
src/main/webapp/add.jsp
1<form action="${pageContext.request.contextPath}/account/save" method="post">
2 <div class="form-group">
3 <label for="name">姓名:</label>
4 <input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名">
5 </div>
6 <div class="form-group">
7 <label for="money">余额:</label>
8 <input type="text" class="form-control" id="money" name="money" placeholder="请输入余额">
9 </div>
10
11 <div class="form-group" style="text-align: center">
12 <input class="btn btn-primary" type="submit" value="提交" />
13 <input class="btn btn-default" type="reset" value="重置" />
14 <input class="btn btn-default" type="button" onclick="history.go(-1)" value="返回" />
15 </div>
16 </form>
- AccountController
1@RequestMapping("/save")
2 public String save(Account account){
3 accountService.save(account);
4 return "redirect:/account/findAll";
5 }
- AccountService 接口和实现类
接口
1package com.soulboy.service;
2
3import com.soulboy.domain.Account;
4
5import java.util.List;
6
7public interface AccountService {
8 public List<Account> findAll();
9 public void save(Account account);
10}
实现类
1package com.soulboy.service.impl;
2
3import com.soulboy.dao.AccountDao;
4import com.soulboy.domain.Account;
5import com.soulboy.service.AccountService;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.stereotype.Service;
8import org.springframework.transaction.annotation.Transactional;
9
10import java.util.List;
11
12@Service
13@Transactional
14public class AccountServiceImpl implements AccountService {
15 @Autowired
16 private AccountDao accountDao;
17
18 @Override
19 public List<Account> findAll() {
20 //System.out.println("findAll执行了……");
21 List<Account> list = accountDao.findAll();
22 return list;
23 }
24
25 @Override
26 public void save(Account account) {
27 accountDao.save(account);
28 }
29}
- AccountDao
1package com.soulboy.dao;
2
3import com.soulboy.domain.Account;
4
5import java.util.List;
6
7public interface AccountDao {
8 /*
9 查询所有
10 */
11 public List<Account> findAll();
12
13 /*
14 增加用户
15 */
16 void save(Account account);
17
18}
- AccountDao.xml 映射
AccountDao.xml
1<?xml version="1.0" encoding="UTF-8" ?>
2<!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5<mapper namespace="com.soulboy.dao.AccountDao">
6 <select id="findAll" resultType="Account">
7 select * from account
8 </select>
9
10 <insert id="save" parameterType="Account">
11 insert into account (name, money) values (#{name}, #{money})
12 </insert>
13</mapper>
- 测试:添加用户
http://localhost:8080/ssm/account/findAll