SSM框架整合demo

SSM框架整合demo,第1张

提示

本demo只是对SSM框架进行整合搭建的一个demo,并不包含其他的功能,只供ssm框架整合借鉴。

先对spring和springMVC框架进行整合
  1. 导入pom依赖
<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.7maven.compiler.source>
    <maven.compiler.target>1.7maven.compiler.target>
    <junit.version>4.11junit.version>
    <spring-webmvc.version>5.3.19spring-webmvc.version>
    <spring-jdbc.version>5.3.16spring-jdbc.version>
    <jsp.version>2.2jsp.version>
    <jstl.version>1.2jstl.version>
    <servlet.version>2.5servlet.version>
    <log4j.version>1.2.17log4j.version>
    <slf4j.version>1.6.6slf4j.version>
    <fastjson.version>1.2.78fastjson.version>
    <mybatis.version>3.5.6mybatis.version>
    <mybatis-spring.version>2.0.7mybatis-spring.version>
    <druid.version>1.2.8druid.version>
    <mysql-connector.version>8.0.27mysql-connector.version>
    <jsr303.version>0.0.2jsr303.version>
    <lombok.version>1.18.22lombok.version>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>${junit.version}version>
      <scope>testscope>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring-webmvc.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring-jdbc.version}version>
    dependency>

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-txartifactId>
      <version>${spring-jdbc.version}version>
    dependency>

    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>jsp-apiartifactId>
      <version>${jsp.version}version>
    dependency>

    <dependency>
      <groupId>jstlgroupId>
      <artifactId>jstlartifactId>
      <version>${jstl.version}version>
    dependency>

    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>servlet-apiartifactId>
      <version>${servlet.version}version>
    dependency>

    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
      <version>${log4j.version}version>
    dependency>

    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-apiartifactId>
      <version>${slf4j.version}version>
    dependency>

    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-log4j12artifactId>
      <version>${slf4j.version}version>
    dependency>

    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>${fastjson.version}version>
    dependency>

    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
      <version>${mysql-connector.version}version>
    dependency>

    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatisartifactId>
      <version>${mybatis.version}version>
    dependency>

    <dependency>
      <groupId>org.mybatisgroupId>
      <artifactId>mybatis-springartifactId>
      <version>${mybatis-spring.version}version>
    dependency>

    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>${druid.version}version>
    dependency>

    <dependency>
      <groupId>fun.fengwk.auto-validationgroupId>
      <artifactId>jsr303artifactId>
      <version>${jsr303.version}version>
    dependency>

    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <version>${lombok.version}version>
    dependency>
  dependencies>

  <build>
    <finalName>ssm_demofinalName>
	
    <resources>
      <resource>
        <directory>src/main/javadirectory>
        <includes>
          <include>**/*.propertiesinclude>
          <include>**/*.xmlinclude>
        includes>
        <filtering>falsefiltering>
      resource>
      <resource>
        <directory>src/main/resourcesdirectory>
        <includes>
          <include>**/*.propertiesinclude>
          <include>**/*.xmlinclude>
        includes>
        <filtering>falsefiltering>
      resource>
    resources>
  build>
  1. web.xml文件配置dispatcherServlet和字符过滤器
<servlet>
    <servlet-name>dispacherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>
    
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>dispacherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

  
  <filter>
    <filter-name>encodingfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
  filter>
  <filter-mapping>
    <filter-name>encodingfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>

  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>
  1. 建立springmvc.xml文件
<context:component-scan base-package="com.demo.ssm.controller"/>

<mvc:annotation-driven/>

<bean id="view" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
bean>


<mvc:default-servlet-handler>mvc:default-servlet-handler>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
  1. 建立各个package,完整demo结构如图所示

  2. 在controller中编写test方法测试springMVC是否能够正常运行

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/getOneAdmin")
    public String test(Model model) {
        Admin admin = adminService.getOneAdmin();
        System.out.println(JSON.toJSONString(admin));
        model.addAttribute("admin", admin);
        return "t";
    }
}

index.jsp文件内容

<body>
    <a href="${pageContext.request.contextPath}/admin/getOneAdmin">测试a>
body>

t.jsp文件内容

<body>
    这里是测试界面...
body>

到此为止,部署tomcat之后能够正常从index.jsp页面跳转到t.jsp页面,说明springMVC搭建成功。

如果页面跳转时出现404页面找不到
在Project Structure下Artifacts中选择项目 ->WEB-INF,在这个文件夹下新建lib文件夹,然后点击+号然后选择所有的library文件并保存,重启服务器即可

整合mybatis

建立applicationContext.xml文件配置mybatis相关的内容



<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    
    <property name="initialSize" value="5"/>
    
    <property name="minIdle" value="5"/>
    
    <property name="maxActive" value="20"/>
    
    <property name="maxWait" value="60000"/>
bean>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    
    <property name="dataSource" ref="dataSource"/>
    
    <property name="typeAliasesPackage" value="com.demo.ssm.pojo">property>
    
    <property name="mapperLocations" value="classpath:mapper/*.xml"/>
bean>


<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.demo.ssm.dao"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
bean>


<context:component-scan base-package="com.demo.ssm">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
context:component-scan>


<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
bean>


<tx:annotation-driven transaction-manager="tx"/>

接下来在service和dao层分别编写一个getOneAdmin方法测试是否能够成功从数据库查询到数据
AdminMapper内容

public interface AdminMapper {
    Admin getOneAdmin();
}

AdminMapper.xml内容

<mapper namespace="com.demo.ssm.dao.AdminMapper">
    <select id="getOneAdmin" resultType="com.demo.ssm.pojo.Admin">
        SELECT * FROM admin LIMIT 1;
    select>
mapper>

AdminService内容

public interface AdminService {
    Admin getOneAdmin();
}

AdminServiceImpl内容

@Service("adminService")
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin getOneAdmin() {
        System.out.println("test......");
        return adminMapper.getOneAdmin();
    }
}

db_library.sql文件内容

Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50729
Source Host           : localhost:3306
Source Database       : db_library

Target Server Type    : MYSQL
Target Server Version : 50729
File Encoding         : 65001

Date: 2022-04-23 23:05:43
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `admin`
-- ----------------------------
DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `username` varchar(20) DEFAULT NULL COMMENT '用户名',
  `password` varchar(20) DEFAULT NULL COMMENT '密码',
  `adminType` int(11) DEFAULT NULL COMMENT '管理员类型',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='管理员';

-- ----------------------------
-- Records of admin
-- ----------------------------
INSERT INTO `admin` VALUES ('1', 'admin', '12345', '1');
INSERT INTO `admin` VALUES ('2', 'test', '12345', '0');

到这里SSM框架整合就完成啦,运行服务器即可测试是否整合成功,后续的功能代码可以在此demo的基础上进行编写。
自己学习的ssm整合的 *** 作,实 *** 了一遍能够正常运行,若小伙伴发现了其他的问题也可以留言交流,共同进步!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/731592.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存