• SSM框架整合步骤详解


    1.1 需求和步骤分析

    需求
    使用ssm框架完成对 account 表的增删改查操作。

    步骤分析
    在这里插入图片描述

    1.2 环境搭建

    1)准备数据库和表记录
    CREATE TABLE `account` ( 
    	`id` int(11) NOT NULL AUTO_INCREMENT, 
    	`name` varchar(32) DEFAULT NULL, 
    	`money` double DEFAULT NULL, 
    	PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    
    
    insert into `account`(`id`,`name`,`money`) values (1,'tom',1000), (2,'jerry',1000);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    2)创建web项目

    创建步骤可参考这篇文章:https://blog.csdn.net/u012660464/article/details/126652568?spm=1001.2014.3001.5501

    1.3 编写mybatis在ssm环境中可以单独使用

    1)相关坐标
    
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.47version>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>1.1.15version>
            dependency>
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.5.1version>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.12version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    2)Account实体
    public class Account {
    
        private Integer id;
        private String name;
        private Double money;
    
        public Account() {
        }
    
        public Account(int id, String name, double money) {
            this.id =id;
            this.name = name;
            this.money = money;
        }
    
        public Account(String name, double money) {
            this.name = name;
            this.money = 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    3)AccountDao接口
    public interface AccountDao {
    
        /*
            查询所有账户
         */
        public List<Account> findAll();
    
        void save(Account account);
    
        Account findById(Integer id);
    
        void update(Account account);
    
        void deleteBatch(Integer[] ids);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    4)AccountDao.xml映射
    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.zwt.dao.AccountDao">
    
        
        <select id="findAll" resultType="account">
            select * from account
        select>
    
        
        <insert id="save" parameterType="account">
            insert into account(name,money)  values(#{name},#{money})
        insert>
    
        
        <select id="findById" parameterType="int" resultType="account">
            select * from account where id = #{id}
        select>
    
        
        <update id="update" parameterType="account">
            update account set name = #{name},money = #{money} where id = #{id}
        update>
    
        
        <delete id="deleteBatch" parameterType="int">
            delete from account
            <where>
                <foreach collection="array" open="id in(" close=")" separator="," item="id">
                    #{id}
                foreach>
            where>
        delete>
    
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    注:xml 包名及结构需要和 java 文件一致

    5)mybatis核心配置文件

    jdbc.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql:///test
    jdbc.username=root
    jdbc.password=zhangwt
    
    • 1
    • 2
    • 3
    • 4

    注:一般 resourse 资源目录下,数据库信息需根据自身情况调整

    SqlMapConfig.xml

    
    DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
        
        
        <properties resource="jdbc.properties"/>
    
        
        <typeAliases> <package name="com.zwt.domain"/> typeAliases>
    
        
        <environments default="dev">
            
            
            <environment id="dev">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url" value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/> 
                    <property name="password" value="${jdbc.password}"/> 
                dataSource> environment> environments> 
        
         
        <mappers> 
            <package name="com.zwt.dao"/> 
        mappers> 
        
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    注:一般 resourse 资源目录下

    6)测试代码
    public class MybatisTest {
    
        @Test
        public void  testMybatis() throws IOException {
    
            InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
    
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            AccountDao mapper = sqlSession.getMapper(AccountDao.class);
    
            List<Account> all = mapper.findAll();
    
            for (Account account : all) {
                System.out.println(account);
            }
    
            sqlSession.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    当前项目结构如下:
    在这里插入图片描述
    运行测试类,可以发现mybatis单独配置成功
    在这里插入图片描述

    1.4 编写spring在ssm环境中可以单独使用

    1)相关坐标
      
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>5.1.5.RELEASEversion>
            dependency>
            <dependency>
                <groupId>org.aspectjgroupId>
                <artifactId>aspectjweaverartifactId>
                <version>1.8.13version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>5.1.5.RELEASEversion>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-txartifactId>
                <version>5.1.5.RELEASEversion>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>5.1.5.RELEASEversion>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    2)AccountService接口
    public interface AccountService {
    
    
        public List<Account> findAll();
    
        void save(Account account);
    
        Account findById(Integer id);
    
        void update(Account account);
    
        void deleteBatch(Integer[] ids);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    3)AccountServiceImpl实现
    @Service
    public class AccountServiceImpl implements AccountService {
    
        /*
            测试spring在ssm环境中的单独使用
         */
        public List<Account> findAll() {
            System.out.println("findAll执行了....");
            return null;
        }
    
        public void save(Account account) {
    
        }
    
        public Account findById(Integer id) {
            return null;
        }
    
        public void update(Account account) {
    
        }
    
        public void deleteBatch(Integer[] ids) {
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    4)spring核心配置文件

    applicationContext.xml

    
    <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:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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/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">
    
    
        
        <context:component-scan base-package="com.zwt.service">context:component-scan>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    5)测试代码
    //替换运行器,指定当前Junit的环境为Spring环境
    @RunWith(SpringJUnit4ClassRunner.class)
    //加载核心配置文件
    @ContextConfiguration("classpath:applicationContext.xml")
    public class SpringTest {
    
        @Autowired
        private AccountService accountService;
    
        @Test
        public void testSpring(){
    
            List<Account> all = accountService.findAll();
            for (Account account : all) {
                System.out.println(account);
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    运行
    在这里插入图片描述
    在这里插入图片描述
    至此,Spring单独配置完成

    1.5 spring整合mybatis

    1)整合思想

    将mybatis接口代理对象的创建权交给spring管理,我们就可以把dao的代理对象注入到service中,此时也就完成了spring与mybatis的整合了。

    2)导入整合包
      
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>1.3.1version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    3)spring配置文件管理mybatis

    注意:此时可以将mybatis主配置文件 SqlMapConfig.xml 删除。
    applicationContext.xml

    
    <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:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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/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">
    
    
        
        <context:component-scan base-package="com.zwt.service">context:component-scan>
    
        
        <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
    
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource">property>
            <property name="typeAliasesPackage" value="com.zwt.domain">property>
    
            
           
        bean>
    
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.zwt.dao">property>
         bean>
        
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    4)修改AccountServiceImpl
    @Service
    public class AccountServiceImpl implements AccountService {
    
        @Autowired
        private AccountDao accountDao;
    
        /*
            测试spring在ssm环境中的单独使用
         */
        public List<Account> findAll() {
            return accountDao.findAll();
        }
    
        public void save(Account account) {
    
        }
    
        public Account findById(Integer id) {
            return null;
        }
    
        public void update(Account account) {
    
        }
    
        public void deleteBatch(Integer[] ids) {
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    5)测试

    在这里插入图片描述
    在这里插入图片描述

    1.6 编写springMVC在ssm环境中可以单独使用

    需求:访问到controller里面的方法查询所有账户,并跳转到list.jsp页面进行列表展示

    1)相关坐标

    pom.xml

      
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>5.1.5.RELEASEversion>
            dependency>
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.1.0version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>javax.servlet.jspgroupId>
                <artifactId>jsp-apiartifactId>
                <version>2.2version>
                <scope>providedscope>
            dependency>
            <dependency>
                <groupId>jstlgroupId>
                <artifactId>jstlartifactId>
                <version>1.2version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    2)导入页面资源

    在这里插入图片描述
    相关页面素材资源已上传,点击链接可下载,https://download.csdn.net/download/u012660464/86507398

    3)前端控制器DispathcerServlet

    web.xml

    
    <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">
             
        
        <servlet>
            <servlet-name>DispatcherServletservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                 
                <param-value>classpath:spring-mvc.xmlparam-value>
            init-param>
            <load-on-startup>2load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>DispatcherServletservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
        
        <filter>
            <filter-name>CharacterEncodingFilterfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>UTF-8param-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    4)AccountController和 list.jsp
    @Controller
    @RequestMapping("/account")
    public class AccountController {
    
    
        /*
            查询所有用户
         */
    
        @RequestMapping("/findAll")
        public  String findAll(Model model){
            List<Account> list = new ArrayList<>();
            list.add(new Account(1,"张三",1000d));
            list.add(new Account(2,"李四",1000d));
            model.addAttribute("list", list);
    
            return "list";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    list.jsp

    <c:forEach items="${list}" var="account">
    
        <tr>
            <td>
                <input type="checkbox" name="ids" value="${account.id}">
            </td>
            <td>${account.id}</td>
            <td>${account.name}</td>
            <td>${account.money}</td>
            <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/account/findById?id=${account.id}">修改</a>&nbsp;<a class="btn btn-default btn-sm" href="">删除</a></td>
        </tr>
    </c:forEach>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    5)springMVC核心配置文件

    spring-mvc.xml

    <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.zwt.controller">context:component-scan>
    
        
        <mvc:annotation-driven>mvc:annotation-driven>
    
        
        <bean id="resourceViewResolve" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/">property>
            <property name="suffix" value=".jsp">property>
        bean>
    
        
        <mvc:default-servlet-handler>mvc:default-servlet-handler>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    6)测试

    在这里插入图片描述

    配置tomcat
    在这里插入图片描述
    在这里插入图片描述
    把当前工程,部署到tomcat上
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    选择application sever
    在这里插入图片描述
    create configuration
    在这里插入图片描述
    在这里插入图片描述
    点击Apply 和 ok
    在这里插入图片描述
    启动tomcat
    在这里插入图片描述
    在浏览器访问:http://localhost:8080/ssmWeb/account/findAll
    在这里插入图片描述

    1.7 spring整合springMVC

    1)整合思想

    spring和springMVC其实根本就不用整合,本来就是一家。
    但是我们需要做到spring和web容器整合,让web容器启动的时候自动加载spring配置文件,web容器销毁的时候spring的ioc容器也销毁。

    2)spring和web容器整合

    ContextLoaderListener加载
    可以使用spring-web包中的ContextLoaderListener监听器,可以监听servletContext容器的创建和销毁,来同时创建或销毁IOC容器。

    web.xml

        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
        listener>
    
        <context-param>
            <param-name>contextConfigLocationparam-name>
            
            <param-value>classpath:applicationContext.xmlparam-value>
        context-param>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    3)修改AccountController
    
    @Controller
    @RequestMapping("/account")
    public class AccountController {
    
        @Autowired
        private AccountService accountService;
    
        /*
            查询所有用户
         */
    
        @RequestMapping("/findAll")
        public  String findAll(Model model){
            List<Account> list = accountService.findAll(); 
            model.addAttribute("list", list);
    
            return "list";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    4)测试

    启动tomcat,使用浏览器访问 http://localhost:8080/ssmWeb/account/findAll
    在这里插入图片描述

    1.8 spring配置声明式事务

    1)spring配置文件加入声明式事务

    applicationContext.xml

        
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource">property>
        bean>
    
        
        <tx:annotation-driven/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    AccountServiceImpl 类添加 @Transactional 注解

    @Service
    @Transactional
    public class AccountServiceImpl implements AccountService {
    	//...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2)add.jsp
                <form action="${pageContext.request.contextPath}/account/save" method="post">
                    <div class="form-group">
                        <label for="name">姓名:label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名">
                    div>
                          <div class="form-group">
                        <label for="money">余额:label>
                        <input type="text" class="form-control" id="money" name="money" placeholder="请输入余额">
                    div>
    
                    <div class="form-group" style="text-align: center">
                        <input class="btn btn-primary" type="submit" value="提交" />
                        <input class="btn btn-default" type="reset" value="重置" />
                        <input class="btn btn-default" type="button" onclick="history.go(-1)" value="返回" />
                    div>
                form>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    3)AccountController

    修改save方法

    @Controller
    @RequestMapping("/account")
    public class AccountController {
    
        @Autowired
        private AccountService accountService;
        
            @RequestMapping("/save")
        public String save(Account account){
    
            accountService.save(account);
            // 跳转到findAll方法重新查询一次数据库进行数据的遍历展示
            return "redirect:/account/findAll";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    4)AccountService接口和实现类

    AccountService.java

    public interface AccountService {
        void save(Account account);
    }
    
    • 1
    • 2
    • 3

    AccountServiceImpl.java

    @Service
    @Transactional
    public class AccountServiceImpl implements AccountService {
    
        //需要用到AccountDao的代理对象
        @Autowired
        private AccountDao accountDao;
    
        /*
            账户添加
         */
        @Override
        public void save(Account account) {
            accountDao.save(account);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    5)AccountDao

    AccountDao.java

    void save(Account account);
    
    • 1
    6)AccountDao.xml映射
        
        <insert id="save" parameterType="account">
            insert into account(name,money)  values(#{name},#{money})
        insert>
    
    • 1
    • 2
    • 3
    • 4
    7)测试

    启动tomcat,使用浏览器访问 http://localhost:8080/ssmWeb/add.jsp
    在这里插入图片描述
    在这里插入图片描述
    至此,SSM框架整合已经完成了!

    为了以后方便,我们再把修改和删除操作完善,做成一个Demo,方便后续参考。

    1.9 修改操作

    1.9.1 数据回显
    ① AccountController
        /*
            根据id查询账户信息,完成账户回显
         */
        @RequestMapping("/findById")
        public String findById(Integer id,Model model){
            Account account =  accountService.findById(id);
            //存到model中
            model.addAttribute("account",account);
            //视图跳转
            return  "update";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    ② AccountService接口和实现类

    AccountService.java

    Account findById(Integer id);
    
    • 1

    AccountServiceImpl.java

        @Override
        public Account findById(Integer id) {
            return accountDao.findById(id);
        }
    
    • 1
    • 2
    • 3
    • 4
    ③ AccountDao接口和映射文件
    Account findById(Integer id);
    
    • 1
        <!--根据ID查询账户信息   Account findById(Integer id);-->
        <select id="findById" parameterType="int" resultType="account">
            select * from account where id = #{id}
        </select>
    
    • 1
    • 2
    • 3
    • 4
    ④ update.jsp
                <form action="${pageContext.request.contextPath}/account/update" method="post">
                    <input type="hidden" name="id" value="${account.id}">
                    <div class="form-group">
                        <label for="name">姓名:</label>
                        <input type="text" class="form-control" id="name" name="name" value="${account.name}" placeholder="请输入姓名">
                    </div>
                    <div class="form-group">
                        <label for="money">余额:</label>
                        <input type="text" class="form-control" id="money" name="money"  value="${account.money}" placeholder="请输入余额">
                    </div>
    
                    <div class="form-group" style="text-align: center">
                        <input class="btn btn-primary" type="submit" value="提交" />
                        <input class="btn btn-default" type="reset" value="重置" />
                        <input class="btn btn-default" type="button" onclick="history.go(-1)" value="返回" />
                    </div>
                </form>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1.9.2 账户更新
    ① AccountController
        /*
            更新账户
         */
        @RequestMapping("/update")
        public String update(Account account){
            accountService.update(account);
            return "redirect:/account/findAll";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    ② AccountService接口和实现类

    AccountService.java

    void update(Account account);
    
    • 1

    AccountServiceImpl.java

    @Override 
    public void update(Account account) { 
    	accountDao.update(account); 
    }
    
    • 1
    • 2
    • 3
    • 4
    ③ AccountDao接口和映射文件

    AccountDao.java

    void update(Account account);
    
    • 1

    AccountDao.xml

    <update id="update" parameterType="Account"> 
    	update account set name = #{name},money = #{money} where id = #{id} update>
    
    • 1
    • 2
    1.9.3 测试

    启动tomcat,使用浏览器访问 http://localhost:8080/ssmWeb/account/findAll
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    1.10 批量删除

    1)list.jsp
    
    <script>
        /*给删除选中按钮绑定点击事件*/
        $('#deleteBatchBtn').click(function () {
                if(confirm('您确定要删除吗')){
                    if($('input[name=ids]:checked').length > 0){
                        /*提交表单*/
                        $('#deleteBatchForm').submit();
                    }
    
                }else {
                    alert('想啥呢,没事瞎操作啥')
                }
        })
    
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    2)AccountController
        /*
            批量删除
         */
        @RequestMapping("/deleteBatch")
        public String deleteBatch(Integer[] ids){
    
            accountService.deleteBatch(ids);
    
            return "redirect:/account/findAll";
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    3)AccountService接口和实现类

    AccountService.java

    void deleteBatch(Integer[] ids);
    
    • 1

    AccountServiceImpl.java

        @Override
        public void deleteBatch(Integer[] ids) {
            
            accountDao.deleteBatch(ids);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    4)AccountDao接口和映射文件

    AccountDao.java

    void deleteBatch(Integer[] ids);
    
    • 1

    AccountDao.xml

        
        <delete id="deleteBatch" parameterType="int">
            delete from account
            <where>
                <foreach collection="array" open="id in(" close=")" separator="," item="id">
                    #{id}
                foreach>
            where>
        delete>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    5)测试

    启动tomcat,使用浏览器访问 http://localhost:8080/ssmWeb/account/findAll
    在这里插入图片描述
    在这里插入图片描述

    至此,整个Demo工程完成,我已上传完整版的源码,可直接下载使用,下载链接:
    https://download.csdn.net/download/u012660464/86507494

  • 相关阅读:
    汽车变排量空调压缩机电磁阀故障诊断
    北斗三号5G遥测终端机系统在水库大坝安全监测应用
    zlib-1.2.11库、libpng-1.6.36库编译及交叉编译 —— 附带shell编译脚本及源码
    工具软件---Linux下安装Arthas
    Linux下QT打开文件选择对话框时,程序报错退出
    go-channel
    React环境搭建
    Opencv图像缩放和平移
    GBase 8c V3.0.0数据类型——网络地址函数和操作符(macaddr函数)
    MySQL系列一:账号管理与引擎
  • 原文地址:https://blog.csdn.net/u012660464/article/details/126652110