从零开始 详细介绍SSM框架的搭建过程 |
您所在的位置:网站首页 › ssm框架使用 › 从零开始 详细介绍SSM框架的搭建过程 |
SSM框架是当前开发Web项目相当火热的技术,整合了Spring,SpringMVC和Mybatis三个框架。下面将详细介绍了从零开始搭建SSM框架的一个过程,分享给大家,有助于更好的搭建ssm,具体如下:
整合说明:SSM整合可以使用多种方式,咱们会选择XML + 注解的方式 整合的思路 2.1. 先搭建整合的环境 2.2. 先把Spring的配置搭建完成 2.3. 再使用Spring整合SpringMVC框架 2.4. 最后使用Spring整合MyBatis框架 创建数据库和表结构 3.1创建数据库 create database ssm; create table account( id int primary key auto_increment, name varchar(20), money double );4.创建maven工程 5.0.2.RELEASE 1.6.6 1.2.12 5.1.6 3.4.5 org.aspectj aspectjweaver 1.6.8 org.springframework spring-aop ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-test ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} junit junit 4.12 test mysql mysql-connector-java ${mysql.version} javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.0 provided jstl jstl 1.2 log4j log4j ${log4j.version} org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring 1.3.0 com.alibaba druid 1.1.10 ssm org.apache.maven.plugins maven-compiler-plugin 3.2 1.8 1.8 UTF-8 true编写实体类,在ssm_domain项目中编写 package com.qcby.entity; import java.io.Serializable; public class Account implements Serializable { // 主键 private int id; // 账户名称 private String name; // 账号的金额 private Double money; public int getId() { return id; } public void setId(int 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接口和实现类 package com.qcby.service; import com.qcby.entity.Account; import java.util.List; public interface AccountService { //查询所有 public List findAll(); } package com.qcby.service; import com.qcby.entity.Account; import org.springframework.stereotype.Service; import java.util.List; @Service public class AccountServiceImpl implements AccountService { //查询所有 @Override public List findAll() { System.out.println("业务层:查询所有"); return null; } } 第二章:Spring框架代码的编写1. 搭建和测试Spring的开发环境 在ssm_web项目中创建spring.xml的配置文件,编写具体的配置信息。 在ssm_web项目中编写测试方法,进行测试 package com.qcby.demo; import com.qcby.service.AccountService; import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring { @Test public void run(){ ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring.xml"); AccountService service = ac.getBean(AccountService.class); service.findAll(); } } 第三章:Spring整合SpringMVC框架 1. 搭建和测试SpringMVC的开发环境在web.xml中配置DispatcherServlet前端控制器 DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc.xml 1 DispatcherServlet /在web.xml中配置DispatcherServlet过滤器解决中文乱码 CharacterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 CharacterEncodingFilter /*创建springmvc.xml的配置文件,编写配置文件 测试SpringMVC的框架搭建是否成功 4.1. 编写index.jsp和list.jsp编写,超链接 Title 查询所有4.2. 创建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 { /** * 查询所有 * @return */ @RequestMapping("/findAll") public ModelAndView findAll(){ System.out.println("表现层:查询所有"); ModelAndView mv = new ModelAndView(); mv.setViewName("suc"); return mv; } } 2. Spring整合SpringMVC的框架目的:在controller中能成功的调用service对象中的方法。 在项目启动的时候,就去加载spring.xml的配置文件,在web.xml中配置 ContextLoaderListener监听器(该监听器只能加载WEB-INF目录下的applicationContext.xml的配置文 件,所以要配置全局的变量加载类路径下的配置文件)。 Archetype Created Web Application org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:spring.xml在controller中注入service对象,调用service对象的方法进行测试 package com.qcby.controller; import com.qcby.entity.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; /** * 查询所有 * @return */ @RequestMapping("/findAll") public ModelAndView findAll(){ System.out.println("表现层:查询所有"); //调用service的方法 List list = accountService.findAll(); ModelAndView mv = new ModelAndView(); mv.setViewName("suc"); return mv; } } 第四章:Spring整合MyBatis框架 1. 搭建和测试MyBatis的环境在web项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件 AccountDao接口 package com.qcby.dao; import com.qcby.entity.Account; import java.util.List; public interface AccountDao { //查询所有 public List findAll(); }编写AccountDao.xml select * from account编写测试的方法 package com.qcby.demo; import com.qcby.dao.AccountDao; import com.qcby.entity.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 TestMybatis { @Test public void run() throws IOException { InputStream in = Resources.getResourceAsStream("mybatis.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in); SqlSession session = factory.openSession(); AccountDao mapper = session.getMapper(AccountDao.class); List list = mapper.findAll(); for (Account account : list) { System.out.println(account); } //关闭资源 session.close(); in.close(); } } 2. Spring整合MyBatis框架目的:把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中 在service中注入dao对象,进行测试 service代码如下 package com.qcby.service; import com.qcby.dao.AccountDao; import com.qcby.entity.Account; 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 findAll() { System.out.println("业务层:查询所有"); List list = accountDao.findAll(); return list; } }controller代码如下 package com.qcby.controller; import com.qcby.entity.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; /** * 查询所有 * @return */ @RequestMapping("/findAll") public ModelAndView findAll(){ System.out.println("表现层:查询所有"); //调用service的方法 List list = accountService.findAll(); for (Account account : list) { System.out.println(account); } ModelAndView mv = new ModelAndView(); mv.setViewName("suc"); return mv; } }spring.xml配置声明式事务管理 表单代码 Title 查询所有 姓名: 金额:controller代码 package com.qcby.controller; import com.qcby.entity.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; /** * 查询所有 * @return */ @RequestMapping("/findAll") public ModelAndView findAll(){ System.out.println("表现层:查询所有"); //调用service的方法 List list = accountService.findAll(); for (Account account : list) { System.out.println(account); } ModelAndView mv = new ModelAndView(); mv.setViewName("suc"); return mv; } @RequestMapping("/save") public String save(Account account){ //Service的保存方法 accountService.save(account); return "suc"; } }service接口和实现类代码 package com.qcby.service; import com.qcby.entity.Account; import java.util.List; public interface AccountService { //查询所有 public List findAll(); //保存 void save(Account account); } package com.qcby.service; import com.qcby.dao.AccountDao; import com.qcby.entity.Account; 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 findAll() { System.out.println("业务层:查询所有"); List list = accountDao.findAll(); return list; } @Override public void save(Account account) { accountDao.save(account); } }dao代码 package com.qcby.dao; import com.qcby.entity.Account; import java.util.List; public interface AccountDao { //查询所有 public List findAll(); //保存 void save(Account account); }dao.xml代码 select * from account insert into account (name,money) values(#{name},#{money})那么,关于SSM框架从零开始搭建的一个详细过程介绍的文章就到此结束了,如果您对SSM框架的知识想要了解更多的内容,请多多关注W3Cschool相关内容的文章,也希望大家可以多多支持! |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |