SSM项目整合

目录

过程:

一、表

account

Sql语句:

二、创建项目

三、pom文件

四、实体类

五、service接口和实现类

AccountService接口

AccountServiceImpl类

六、spring框架代码

1.applicationContext.xml

七、spring整合springMVC

1.搭建和测试spring MVC的开发环境

(1)在web.xml中配置DispatcherServlet前端控制器和DispatcherServlet过滤器解决中文乱码

(2)创建spring-mvc.xml的配置文件

(3)测试springMVC是否搭建成功

①index.jsp

②AccountController类

(4)运行

2.spring整合springMVC

(2)在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件)。

(3)controller中注入service对象,调用service对象的方法进行测试

(4)启动tomcat

八、spring整合mybatis

1.搭建和测试mybatis的环境

(1)mybatis-config.xml

(2)AccountDao接口

(3)AccountDao.xml

(4)测试类

(5)运行

2.spring整合mybatis

(1)把mybatis-config.xml配置文件的内容配置到applicationContext.xml配置文件中

applicationContext.xml

(2)在AccountDao接口中添加@Repository注解

(3)在service中注入dao对象,进行测试

AccountServiceImpl

AccountController

配置声明式事务管理applicationContext.xml

(4)表单

(5)AccountDao接口

(6)service

①AccountService接口

②AccountServiceImpl实现类

(7)AccountController

(8)运行


过程:

1.先搭建整体环境

2.spring配置搭建

3.spring整合springMVC

4.spring整合mybatis

一、表

account

Sql语句:

create database ssm;
​
create table account(
    id int primary key auto_increment,
    name varchar(20),
    money double
);

二、创建项目

三、pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qcby</groupId>
    <artifactId>ssm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <spring.version>5.0.2.RELEASE</spring.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <mysql.version>5.1.6</mysql.version>
        <mybatis.version>3.4.5</mybatis.version>
    </properties>
    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log end -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>ssm</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

四、实体类

Account类

package com.qcby.pojo;

import java.io.Serializable;

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 +
                '}';
    }
}

五、service接口和实现类

AccountService接口

package com.qcby.service;

import com.qcby.pojo.Account;

import java.util.List;

public interface AccountService {
    
    /*
    * 查询所有
    * */
    public List<Account> findAll();
}

AccountServiceImpl类

package com.qcby.service.impl;

import com.qcby.pojo.Account;
import com.qcby.service.AccountService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    /*
    * 查询所有
    * */
    @Override
    public List<Account> findAll() {
        System.out.println("业务层:查询所有账号");
        return null;
    }
}

六、spring框架代码

1.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--开启注解扫描,要扫描的是service-->
    <context:component-scan base-package="com.qcby.service"/>


</beans>

七、spring整合springMVC

1.搭建和测试spring MVC的开发环境

(1)在web.xml中配置DispatcherServlet前端控制器DispatcherServlet过滤器解决中文乱码

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">


    <!--解决post请求中文乱码的过滤器-->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!--配置前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--加载spring-mvc.xml配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!--启动加载-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>

(2)创建spring-mvc.xml的配置文件

<?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">

    <!-- 扫描controller的注解,别的不扫描 -->
    <context:component-scan base-package="com.qcby.controller"></context:component-scan>

    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 配置spring开启注解mvc的支持-->
        <mvc:annotation-driven />
</beans>

(3)测试springMVC是否搭建成功

①index.jsp
<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2024/11/12
  Time: 17:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h3>账号列表</h3>
<a href="/account/findAll.do">查询所有</a>
</body>
</html>
②AccountController类
package com.qcby.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/account")
public class AccountController {

    /*
    * 查询所有
    * */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        System.out.println("表现层:查询所有");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("success");
        return mv;
    }
}

(4)运行

查询所有

控制台:

2.spring整合springMVC

(1)目的:在controller中能成功的调用service对象中的方法。

(2)在项目启动的时候,就去加载applicationContext.xml的配置文件,在web.xml中配置ContextLoaderListener监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文件)。

<!--配置spring的监听器-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置加载类路径的配置文件-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

(3)controller中注入service对象,调用service对象的方法进行测试

package com.qcby.controller;

import com.qcby.pojo.Account;
import com.qcby.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.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    //注入
    @Autowired
    private AccountService accountService;

    /*
    * 查询所有
    * */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        System.out.println("表现层:查询所有账号");

        //调用service对象的方法
        List<Account> list=accountService.findAll();

        ModelAndView mv = new ModelAndView();
        mv.setViewName("success");
        return mv;
    }
}

(4)启动tomcat

查询所有

控制台

八、spring整合mybatis

1.搭建和测试mybatis的环境

(1)mybatis-config.xml

<?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>
        <package name="com.qcby.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
                <property name="username" value="root"/>
                <property name="password" value="2020"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.qcby.dao"/>
        <!--        <mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
        <!-- <mapper resource="mapper/UserMapper.xml"></mapper> -->
    </mappers>
</configuration>

(2)AccountDao接口

package com.qcby.dao;

import com.qcby.pojo.Account;

import java.util.List;

public interface AccountDao {
    
    public List<Account> findAll();
}

(3)AccountDao.xml

<?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.qcby.dao.AccountDao"> <!--对谁进行操作就写谁-->

    <!--查询所有:public List<Account> findAll();-->
    <select id="findAll" resultType="com.qcby.pojo.Account">
        select * from account
    </select>
</mapper>

(4)测试类

package com.qcby.test;

import com.qcby.dao.AccountDao;
import com.qcby.pojo.Account;
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.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyBatisTest {
    
    @Test
    public void run() throws IOException {
        //加载配置文件
        InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
        //构建工厂
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //创建session
        SqlSession session=sqlSessionFactory.openSession();
        //获取代理类方法
        AccountDao accountMapper=session.getMapper(AccountDao.class);

        //调用方法
        List<Account> list=accountMapper.findAll();
        for(Account account:list){
            System.out.println(account);
        }

        //关闭资源
        session.close();
        inputStream.close();
    }
}

(5)运行

2.spring整合mybatis

(1)把mybatis-config.xml配置文件的内容配置到applicationContext.xml配置文件中

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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--开启注解扫描,要扫描的是service-->
    <context:component-scan base-package="com.qcby.service"/>

    <!--配置druid连接池-->
    <!--配置开源连接池 Druid连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///ssm"/>
        <property name="username" value="root"/>
        <property name="password" value="2020"/>
    </bean>

    <!--spring整合mybatis-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入连接池对象-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载映射配置文件-->
        <property name="mapperLocations" value="classpath:com/qcby/dao/*.xml"/>
    </bean>

    <!--把dao对象存到IOC容器中-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactory" ref="sessionFactory" />
        <!--扫码dao所在的包,把包中接口的代码对象存入到IOC容器中-->
        <property name="basePackage" value="com.qcby.dao" />
    </bean>

    <!--平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入连接池-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置事务的通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find" read-only="true"/>
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>

    <!--配置事务的增强-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.qcby.service.*ServiceImpl.*(..))"/>
    </aop:config>


</beans>

(2)在AccountDao接口中添加@Repository注解

package com.qcby.dao;

import com.qcby.pojo.Account;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AccountDao {

    public List<Account> findAll();
}

(3)在service中注入dao对象,进行测试

AccountServiceImpl
package com.qcby.service.impl;

import com.qcby.dao.AccountDao;
import com.qcby.pojo.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    
    @Autowired
    private AccountDao accountDao;
    
    /*
    * 查询所有
    * */
    @Override
    public List<Account> findAll() {
        System.out.println("业务层:查询所有账号");
        return accountDao.findAll();
    }
}
AccountController
package com.qcby.controller;

import com.qcby.pojo.Account;
import com.qcby.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.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    //注入
    @Autowired
    private AccountService accountService;

    /*
    * 查询所有
    * */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        System.out.println("表现层:查询所有账号");

        //调用service对象的方法
        List<Account> list=accountService.findAll();
        for (Account account : list){
            System.out.println(account);
        }

        ModelAndView mv = new ModelAndView();
        mv.setViewName("success");
        return mv;
    }
}
配置声明式事务管理applicationContext.xml
<!--配置声明式事务管理-->
<!--平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入连接池-->
    <property name="dataSource" ref="dataSource"/>
</bean>

<!--配置事务的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="find" read-only="true"/>
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>

<!--配置事务的增强-->
<aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.qcby.service.*ServiceImpl.*(..))"/>
</aop:config>

(4)表单

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2024/11/12
  Time: 17:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h3>账号列表</h3>
<a href="/account/findAll.do">查询所有</a>

<h3>测试新增</h3>
<form action="/account/save.do" method="post">
    账号名称:<input type="text" name="name"><br>
    账号金额:<input type="text" name="money"><br>
    <input type="submit" value="保存">
</form>
</body>
</html>

(5)AccountDao接口

这里用了注解,所以需要把AccountDao.xml文件里的东西注释掉

package com.qcby.dao;

import com.qcby.pojo.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AccountDao {

    /*
    * 查询所有
    * */
    @Select("select * from account")
    public List<Account> findAll();
    
    /*
    * 保存
    * */
    @Insert("insert into account(name,money) values(#{name},#{money})")
    public void save(Account account);
}

(6)service

①AccountService接口
package com.qcby.service;

import com.qcby.pojo.Account;

import java.util.List;

public interface AccountService {

    /*
    * 查询所有
    * */
    public List<Account> findAll();

    /*
    * 保存
    * */
    public void save(Account account);
}
②AccountServiceImpl实现类
package com.qcby.service.impl;

import com.qcby.dao.AccountDao;
import com.qcby.pojo.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    /*
    * 查询所有
    * */
    @Override
    public List<Account> findAll() {
        System.out.println("业务层:查询所有账号");
        return accountDao.findAll();
    }

    @Override
    public void save(Account account) {
        accountDao.save(account);
    }
}

(7)AccountController

package com.qcby.controller;

import com.qcby.pojo.Account;
import com.qcby.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.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    //注入
    @Autowired
    private AccountService accountService;

    /*
    * 查询所有
    * */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        System.out.println("表现层:查询所有账号");

        //调用service对象的方法
        List<Account> list=accountService.findAll();
        for (Account account : list){
            System.out.println(account);
        }

        ModelAndView mv = new ModelAndView();
        mv.setViewName("success");
        return mv;
    }

    /*
    * 保存/新增
    * */
    @RequestMapping("/save.do")
    public String save(Account account){
        //调用service保存数据
        accountService.save(account);
        return "success";
    }
}

(8)运行

保存

查看数据库

插入成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值