SSM框架搭建整合环境的方法
这篇"SSM框架搭建整合环境的方法"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"SSM框架搭建整合环境的方法"文章吧。
1. 搭建整合环境
整合说明: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;@Servicepublic 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编写,超链接
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 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
编写测试的方法
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;@Servicepublic 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配置声明式事务管理
表单代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 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;@Servicepublic 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代码
insert into account (name,money) values(#{name},#{money}) 以上就是关于"SSM框架搭建整合环境的方法"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。