猿创征文|Spring、SpringMVC、Mybatis整合测试

目录

SSM整合测试

①创建数据库

②创建maven工程

 ③pom.xml中导入对应的坐标

④编写实体类

⑤编写Mapper接口(dao接口)

 ⑥编写service接口

 ⑦编写Service接口实现

 ⑧编写Controller

 ⑨编写jsp添加页面

 ⑩编写列表jsp展示页面

①①编写响应的配置文件

applicationContext.xml

jdbc.properties

spring-mvc.xml

sqlMapConfig.xml

sqlMapConfig-spring.xml

①②测试添加账户和账户列表

①③数据库中测试


SSM整合测试

①创建数据库

②创建maven工程

 ③pom.xml中导入对应的坐标

  
    
    
      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
    

  

④编写实体类

public class Account {

    private Integer id;
    private String name;
    private Double money;

//get set方法
}

⑤编写Mapper接口(dao接口)


public interface AccountMapper {
   @Insert(" insert into account values(#{id},#{name},#{money})")
    public void save(Account account);

@Select(" select * from account")
    public List findAll();

}

 ⑥编写service接口



public interface AccountService {

    public void save(Account account);

    public List findAll();

}

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

 ⑧编写Controller



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

    @Autowired
    private AccountService accountService;

    //保存
    @RequestMapping(value = "/save",produces = "text/html;charset=UTF-8")
@ResponseBody
    public String save(Account account){
        accountService.save(account);
        return  "提交成功";
    }

    //查询
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        List accountList =  accountService.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("accountList",accountList);
        modelAndView.setViewName("accountList");
        return modelAndView;
    }

}

 ⑨编写jsp添加页面

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


    Title


    

添加账户信息表单

账户名称:
账户金额:

 ⑩编写列表jsp展示页面

<%@ 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}

①①编写响应的配置文件

applicationContext.xml




    
    
        
        
    

    
    

    
    
        
        
        
        
    

    
    
        
        
        
    

    
    
        
    


    
    
    
        
    

    
    
        
            
        
    

    
    
        
    

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123456


spring-mvc.xml




    
    
    
    

    
        
        
    
    
    



sqlMapConfig.xml





    
    



    
    
        
            
            
                
                
                
                
            
        
    

    
    

        
    



sqlMapConfig-spring.xml





    
    
        
        
    

①②测试添加账户和账户列表

①③数据库中测试

你可能感兴趣的:(SSM框架,mybatis,spring,java,springmvc,maven)