目录
1.原始方式整合准备工作
1.1.原始方式整合
1.2.创建maven工程
1.3.导入Maven坐标
1.4.编写实体类
1.5.编写Mapper接口
1.6.编写Service接口
1.7.编写Service接口实现
1.8.编写Controller
1.9.编写添加页面
1.10.编写列表页面
1.11.编写相应配置文件
1.12.测试添加账户
1.13.测试账户列表
2.Mybatis整合Spring(比原始开发改变的相关东西)
2.1.service接口实现
2.2.Spring配置文件:applicationContext.xml
2.3.MyBatis核心配置文件:sqlMapConfig.xml
1.原始方式整合准备工作
1.1.原始方式整合
CREATE TABLE account(
id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAr(100),
money DOUBLE(7,2)
) DEFAULT CHARSET UTF8 COMMENT '';
1.2.创建maven工程
CREATE TABLE account(
id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAr(100),
money DOUBLE(7,2)
) DEFAULT CHARSET UTF8 COMMENT '';
1.2.创建maven工程
1.3.导入Maven坐标
org.springframework
spring-context
5.3.20
org.aspectj
aspectjweaver
1.8.7
org.springframework
spring-jdbc
5.3.20
org.springframework
spring-tx
5.3.20
org.springframework
spring-test
5.3.20
org.springframework
spring-webmvc
5.3.20
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.0
org.mybatis
mybatis
3.5.8
org.mybatis
mybatis-spring
1.3.1
mysql
mysql-connector-java
8.0.29
c3p0
c3p0
0.9.1.2
junit
junit
4.13.1
jstl
jstl
1.2
1.4.编写实体类
package com.caoyan.domain;
public class Account {
private Integer id;
private String name;
private double money;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return this.money;
}
public void setMoney(double money) {
this.money = money;
}
}
1.5.编写Mapper接口
package com.caoyan.mapper;
import java.util.List;
import com.caoyan.domain.Account;
public interface AccountMapper {
// 保存账户数据
public void save(Account account);
// 查询账户数据
public List findAll();
}
1.6.编写Service接口
package com.caoyan.service;
import java.util.List;
import com.caoyan.domain.Account;
public interface AccountService {
// 保存账户数据
public void save(Account account);
// 查询账户数据
public List findAll();
}
1.7.编写Service接口实现
package com.caoyan.service.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.caoyan.domain.Account;
import com.caoyan.mapper.AccountMapper;
import com.caoyan.service.AccountService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Override
public void save(Account account) {
// 保存
try {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
accountMapper.save(account);
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public List findAll() {
// 查询
InputStream resourceAsStream;
try {
resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
List accountList = accountMapper.findAll();
sqlSession.close();
return accountList;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
1.8.编写Controller
package com.caoyan.controller;
import java.util.List;
import com.caoyan.domain.Account;
import com.caoyan.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
// 保存
@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
@ResponseBody
public String save(Account account) {
accountService.save(account);
return "保存成功!";
}
// 查询
@RequestMapping("/findAll")
public ModelAndView findAll() {
List accountList = accountService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("accountList", accountList);
modelAndView.setViewName("accountList");
return modelAndView;
}
}
1.9.编写添加页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
个人账户信息
保存账户信息表单
package com.caoyan.domain;
public class Account {
private Integer id;
private String name;
private double money;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return this.money;
}
public void setMoney(double money) {
this.money = money;
}
}
1.5.编写Mapper接口
package com.caoyan.mapper;
import java.util.List;
import com.caoyan.domain.Account;
public interface AccountMapper {
// 保存账户数据
public void save(Account account);
// 查询账户数据
public List findAll();
}
1.6.编写Service接口
package com.caoyan.service;
import java.util.List;
import com.caoyan.domain.Account;
public interface AccountService {
// 保存账户数据
public void save(Account account);
// 查询账户数据
public List findAll();
}
1.7.编写Service接口实现
package com.caoyan.service.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.caoyan.domain.Account;
import com.caoyan.mapper.AccountMapper;
import com.caoyan.service.AccountService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Override
public void save(Account account) {
// 保存
try {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
accountMapper.save(account);
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public List findAll() {
// 查询
InputStream resourceAsStream;
try {
resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
List accountList = accountMapper.findAll();
sqlSession.close();
return accountList;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
1.8.编写Controller
package com.caoyan.controller;
import java.util.List;
import com.caoyan.domain.Account;
import com.caoyan.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
// 保存
@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
@ResponseBody
public String save(Account account) {
accountService.save(account);
return "保存成功!";
}
// 查询
@RequestMapping("/findAll")
public ModelAndView findAll() {
List accountList = accountService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("accountList", accountList);
modelAndView.setViewName("accountList");
return modelAndView;
}
}
1.9.编写添加页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
个人账户信息
保存账户信息表单
package com.caoyan.service;
import java.util.List;
import com.caoyan.domain.Account;
public interface AccountService {
// 保存账户数据
public void save(Account account);
// 查询账户数据
public List findAll();
}
1.7.编写Service接口实现
package com.caoyan.service.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import com.caoyan.domain.Account;
import com.caoyan.mapper.AccountMapper;
import com.caoyan.service.AccountService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Override
public void save(Account account) {
// 保存
try {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
accountMapper.save(account);
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public List findAll() {
// 查询
InputStream resourceAsStream;
try {
resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
List accountList = accountMapper.findAll();
sqlSession.close();
return accountList;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
1.8.编写Controller
package com.caoyan.controller;
import java.util.List;
import com.caoyan.domain.Account;
import com.caoyan.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
// 保存
@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
@ResponseBody
public String save(Account account) {
accountService.save(account);
return "保存成功!";
}
// 查询
@RequestMapping("/findAll")
public ModelAndView findAll() {
List accountList = accountService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("accountList", accountList);
modelAndView.setViewName("accountList");
return modelAndView;
}
}
1.9.编写添加页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
个人账户信息
保存账户信息表单
package com.caoyan.controller;
import java.util.List;
import com.caoyan.domain.Account;
import com.caoyan.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
// 保存
@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
@ResponseBody
public String save(Account account) {
accountService.save(account);
return "保存成功!";
}
// 查询
@RequestMapping("/findAll")
public ModelAndView findAll() {
List accountList = accountService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("accountList", accountList);
modelAndView.setViewName("accountList");
return modelAndView;
}
}
1.9.编写添加页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
个人账户信息
保存账户信息表单
1.10.编写列表页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
展示账户数据列表
| 账户id | 账户名称 | 账户金额 |
|---|---|---|
| ${account.id} | ${account.name} | ${account.money} |
1.11.编写相应配置文件
- Spring配置文件:applicationContext.xml(第三步)
- SpringMVC配置文件:spring-mvc.xml(第四步)
- MyBatis映射文件:AccountMapper.xml(第一步)
insert into account values(#{id},#{name},#{money})
select * from account
- MyBatis核心文件:sqlMapConfig.xml(第二步)
- 数据库连接信息文件:jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123456
- Web.xml文件:web.xml(第五步)
contextConfigLocation
classpath:applicationContext.xml
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncodingFilter
/*
org.springframework.web.context.ContextLoaderListener
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
DispatcherServlet
/
1.12.测试添加账户
1.13.测试账户列表
2.Mybatis整合Spring(比原始开发改变的相关东西)
2.1.service接口实现
package com.caoyan.service.impl;
import java.util.List;
import com.caoyan.domain.Account;
import com.caoyan.mapper.AccountMapper;
import com.caoyan.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public void save(Account account) {
accountMapper.save(account);
}
@Override
public List findAll() {
return accountMapper.findAll();
}
}
2.2.Spring配置文件:applicationContext.xml
2.3.MyBatis核心配置文件:sqlMapConfig.xml
package com.caoyan.service.impl;
import java.util.List;
import com.caoyan.domain.Account;
import com.caoyan.mapper.AccountMapper;
import com.caoyan.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public void save(Account account) {
accountMapper.save(account);
}
@Override
public List findAll() {
return accountMapper.findAll();
}
}



