create database ssm;
create table account(
id int primary key auto_increment,
name varchar(100),
money double(7,2)
);
package com.tong.domain;
public class Account {
private Integer id;
private String name;
private Double money;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + 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;
}
}
mapper是实现最底层功能的类
mapper是提供给service来使用的,通过mapper的基础方法service通过使用基础方法得到一些高级的方法来让controller使用
controller是直接与页面交互的 controller通过使用service里面的方法来实现具体的功能
这里mapper里面是接口是因为我们可以通过mybaits来自动实现实现类
package com.tong.mapper;
import com.tong.domain.Account;
import java.util.List;
public interface AccountMapper {
public void save(Account account);
public List<Account> findAll();
}
这次实现的方法比较简单所有没有实现额外的功能
接口在这
package com.tong.service;
import com.tong.domain.Account;
import java.util.List;
public interface AccountService {
public void save(Account account );
public List<Account> findAll();
}
实现在这
package com.tong.service.impl;
import com.tong.domain.Account;
import com.tong.mapper.AccountMapper;
import com.tong.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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
通过 在spring容器中找到AccountMapper类来注入到这里面
@Override
public void save(Account account) {
accountMapper.save(account);
}
@Override
public List<Account> findAll() {
return accountMapper.findAll();
}
}
package com.tong.controller;
import com.tong.domain.Account;
import com.tong.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;
import java.util.List;
@Controller
这个注解表明了这个是Controller
@RequestMapping("/account")
这个表示这个下面的方法访问的地址前都要加上/account
public class AccountController {
@Autowired
默认按byType自动装配
public AccountService accountService;
@RequestMapping(value = "/save",produces = "text/html;charset=UTF-8")
这个方法访问是 根目录+/account/save
@ResponseBody
不返回地址
public String save(Account account){
System.out.println(account.toString());
accountService.save(account);
调用service里面的方法完成功能
return "保存成功了";
}
@RequestMapping("/findAll")
public ModelAndView findAll(){
List<Account> all = accountService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("accountList",all);
modelAndView.setViewName("accountList");
modelAndView里面model是封装数据 view是跳转到那个页面
return modelAndView;
}
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tong.mapper.AccountMapper">
这很重要 这确定了我们写的sql语句是要用来实现那个接口位置不能写错
<insert id="save" parameterType="account">
insert into account values(#{id},#{name},#{money})
insert>
<select id="findAll" resultType="account">
select * from account
select>
mapper>
<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.tong">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller">context:exclude-filter>
我们要排除controller是因为controller是直接与页面交互的
controller要被springMVC扫描
context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}">property>
<property name="jdbcUrl" value="${jdbc.url}">property>
<property name="user" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
通过mybatis得到接口的实现类
<property name="configLocation" value="classpath:sqlMapConfig-spring.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.tong.mapper">property>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.tong.service.impl.*.*(..))">aop:advisor>
aop:config>
beans>
<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.tong.controller">context:component-scan>
<mvc:annotation-driven>mvc:annotation-driven>
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:default-servlet-handler>mvc:default-servlet-handler>
beans>
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.tong.domain"/>
typeAliases>
<mappers>
<mapper resource="com.tong.mapper/AccountMapper.xml"/>
这是加载映射文件 这个文件就是之前编写sql语句的xml配置文件
这样mybaits就能自动生成实现类了
mappers>
configuration>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Titletitle>
head>
<body>
<h1>添加账户信息表单h1>
<form name="accountForm" action="${pageContext.request.contextPath}/account/save" method="post">
账户名称:<input type="text" name="name"><br>
账户金额:<input type="text" name="money"><br>
<input type="submit" value="保存"><br>
form>
body>
html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Titletitle>
head>
<body>
<h1>展示账户数据列表h1>
<table border="1">
<tr>
<th>账户idth>
<th>账户名称th>
<th>账户金额th>
tr>
<c:forEach items="${accountList}" var="account">
<tr>
<td>${account.id}td>
<td>${account.name}td>
<td>${account.money}td>
tr>
c:forEach>
table>
body>
html>