文章目录
创建数据库
create database ssm;
create table account(
id int primary key auto_increment,
name varchar(100),
money double(7,2)
);
创建好是这样的
里面的数据是后来加的
创建Maven工程
编写实体类
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是实现最底层功能的类
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();
}
编写service
这次实现的方法比较简单所有没有实现额外的功能
接口在这
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();
}
}
与页面交互的Controller
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;
}
}
sql语句配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!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>
spring 配置文件
<?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">
<!--组件扫描 扫描service和mapper-->
<context:component-scan base-package="com.tong">
<!--排除controller的扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
我们要排除controller是因为controller是直接与页面交互的
controller要被springMVC扫描
</context:component-scan>
<!--加载propeties文件-->
<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>
<!--配置sessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--加载mybatis核心文件-->
通过mybatis得到接口的实现类
<property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
</bean>
<!-- 扫描mapper所在的包 为mapper创建实现类-->
<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织入-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.tong.service.impl.*.*(..))"></aop:advisor>
</aop:config>
</beans>
spring-mvc配置文件
<?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">
<!-- mvc组件扫描 扫描controller-->
<context:component-scan base-package="com.tong.controller"></context:component-scan>
<!--mvc注解驱动-->
<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>
mybaits配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!-- 定义别名-->
<!-- <typeAlias type="com.tong.domain.Account" alias="account"/>-->
<!-- 扫包自动生成别名 别名是当前名字或者首字母小写-->
<package name="com.tong.domain"/>
</typeAliases>
<mappers>
<mapper resource="com.tong.mapper/AccountMapper.xml"/>
这是加载映射文件 这个文件就是之前编写sql语句的xml配置文件
这样mybaits就能自动生成实现类了
<!-- <package name="com.tong.mapper"/>-->
</mappers>
</configuration>
web页面
save页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</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>
accountList页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>展示账户数据列表</h1>
<table border="1">
<tr>
<th>账户id</th>
<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>