mybatis|SSM框架整合(Spring+SpringMVC+Mybatis)

1、创建数据库 mybatis|SSM框架整合(Spring+SpringMVC+Mybatis)
文章图片

2、创建表 mybatis|SSM框架整合(Spring+SpringMVC+Mybatis)
文章图片

3、创建maven工程 导入坐标

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

4、编写实体类
package com.xmp.domain; public class Account {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; } }

5、编写Mapper接口
package com.xmp.mapper; import com.xmp.domain.Account; import java.util.List; public interface AccountMapper {public void save(Account account); public List findAll(); }

6、编写Service接口
package com.xmp.service; import com.xmp.domain.Account; import java.util.List; public interface AccountService {public void save(Account account); public List findAll(); }

7、编写Service接口实现
package com.xmp.service.impl; import com.xmp.domain.Account; import com.xmp.mapper.AccountMapper; import com.xmp.service.AccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @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
package com.xmp.controller; import com.xmp.domain.Account; import com.xmp.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 @RequestMapping("/account") public class AccountController {@Autowired private AccountService accountService; //保存 @RequestMapping(value = "https://www.it610.com/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; }}

9、编写添加页面
Title - 锐客网 添加账户信息表单
账户名称:
账户金额:


10、编写列表页面
Title - 锐客网 展示账户数据列表
账户id 账户名称 账户金额
${account.id} ${account.name} ${account.money}

11、编写相应配置文件
  • Spring配置文件:applicationContext.xml
    【mybatis|SSM框架整合(Spring+SpringMVC+Mybatis)】

  • SprngMVC配置文件:spring-mvc.xml

  • MyBatis映射文件:AccountMapper.xml
    insert into account values(#{id},#{name},#{money}) select * from account

  • MyBatis核心文件:sqlMapConfig-spring.xml

  • 数据库连接信息文件:jdbc.properties
    jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm jdbc.username=root jdbc.passwords=root

  • Web.xml文件:web.xml
    contextConfigLocationclasspath:applicationContext.xml org.springframework.web.context.ContextLoaderListener DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocationclasspath:spring-mvc.xml 1DispatcherServlet / CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encodingUTF-8 CharacterEncodingFilter /*

  • 日志文件:log4j.xml
    # # 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

    推荐阅读