SSM框架整合

heima老师整理

 

SSM框架整合

原始方式整合

1. 创建表结构
2. 创建Maven工程
3. 导入Maven坐标
4. 编写实体类
5. 编写Mapper接口
6. 编写Service接口
7. 编写Service接口实现
8. 编写Controller
9. 编写添加页面
10. 编写列表页面
11. 编写相应配置文件
12. 测试添加账户
13. 测试账户列表

1.准备工作

 CREATE TABLE account(
	id INT PRIMARY KEY AUTO_INCREMENT,
	NAME VARCHAR(100),
	money DOUBLE(7,2)
 )
 
INSERT INTO account VALUES(NULL,"tom",5000);
INSERT INTO account VALUES(NULL,"lucy",5000);

2.创建Maven工程

SSM框架整合_第1张图片

3.导入Maven坐标




    4.0.0

    com.itheima
    itheima_ssm
    1.0-SNAPSHOT
    war

    itheima_ssm Maven Webapp
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
            org.aspectj
            aspectjweaver
            1.8.7
        
        
            org.springframework
            spring-jdbc
            5.0.5.RELEASE
        
        
            org.springframework
            spring-tx
            5.0.5.RELEASE
        
        
            org.springframework
            spring-test
            5.0.5.RELEASE
        
        
            org.springframework
            spring-webmvc
            5.0.5.RELEASE
        

        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet.jsp
            jsp-api
            2.0
        

        
        
            org.mybatis
            mybatis
            3.4.5
        
        
            org.mybatis
            mybatis-spring
            1.3.1
        
        
            mysql
            mysql-connector-java
            5.1.6
        
        
            c3p0
            c3p0
            0.9.1.2
        

        
            junit
            junit
            4.12
        
        
            jstl
            jstl
            1.2
        

    

    
        itheima_ssm
        
            
                
                    maven-clean-plugin
                    3.0.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.7.0
                
                
                    maven-surefire-plugin
                    2.20.1
                
                
                    maven-war-plugin
                    3.2.0
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
            
        
    

4.编写实体类

public class Account {
    private int id;
    private String name;
    private double money;
    //省略getter和setter方法
}

5.编写Mapper接口

public interface AccountMapper {
    //保存账户数据
    void save(Account account);
    //查询账户数据
    List findAll();
}

6.编写Service接口

public interface AccountService {
    void save(Account account); //保存账户数据
    List findAll(); //查询账户数据
}

7.编写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();
    }
}

8.编写Controller

@Controller
public class AccountController {
    @Autowired
    private AccountService accountService;
    @RequestMapping("/save")
    @ResponseBody
    public String save(Account account){
        accountService.save(account);
        return "save success";
    }
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("accountList");
        modelAndView.addObject("accountList",accountService.findAll());
        return modelAndView;
    }
}

9.编写添加页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


    

保存账户信息表单

    
        用户名称
        账户金额
        
    

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}

11. 编写相应配置文件

applicationContext.xml



    
    
        
        
    

    
    

    
    
        
        
        
        
    

    
    
        
        
    

    
    
        
    

    
    
    
        
    

    
    
        
            
        
    

    
    
        
    

    

db.properties

#数据库连接信息
jdbc.url=jdbc:mysql:///ssm
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.pwd=123456

log4j.properties

#
# Hibernate, Relational Persistence for Idiomatic Java
#
# License: GNU Lesser General Public License (LGPL), version 2.1 or later.
# See the lgpl.txt file in the root directory or .
#

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=all, stdout

spring-mvc.xml



    
    
    
    
    
    
        
        
        
    
    
    

    

web.xml



    
    
        
        
    

    
    

    
    
        
        
        
        
    

    
    
        
        
    

    
    
        
    

    
    
    
        
    

    
    
        
            
        
    

    
    
        
    

    

AccountMapper.xml





    
        insert into account values(null,#{name},#{money})
    

    


12. 测试添加账户

13. 测试账户列表 

你可能感兴趣的:(SSM)