• 如何搭建一个基础的springmvc+mybatis项目


    环境
    IDEA+Tomca9+Maven+mybatis

    先进行maven依赖的导入,pom.xml文件的修改

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>ssmgroupId>
        <artifactId>ssmartifactId>
        <version>1.0-SNAPSHOTversion>
        <dependencies>
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-log4j12artifactId>
                <version>1.6.1version>
            dependency>
    
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-apiartifactId>
                <version>1.6.1version>
            dependency>
    
    
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.0.1version>
                <scope>providedscope>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>4.3.13.RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.2.8version>
            dependency>
    
    
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatis-springartifactId>
                <version>1.2.2version>
            dependency>
    
    
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.32version>
            dependency>
    
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-jdbcartifactId>
                <version>4.3.11.RELEASEversion>
            dependency>
    
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-aspectsartifactId>
                <version>4.3.11.RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>2.7.4version>
            dependency>
    
        dependencies>
    
    
        <build>
    
            <finalName>ssmfinalName>
            <resources>
                <resource>
                    <directory>src/main/javadirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                    includes>
                resource>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*.xmlinclude>
                        <include>**/*.propertiesinclude>
                    includes>
                resource>
            resources>
        build>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    web.xml文件的修改

    
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
    
    
        <context-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-config.xmlparam-value>
        context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
        listener>
    
        <servlet>
            <servlet-name>springMVCservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        servlet>
    
        <servlet-mapping>
            <servlet-name>springMVCservlet-name>
            <url-pattern>*.htmlurl-pattern>
        servlet-mapping>
    
    
        <filter>
            <filter-name>encodingFilterfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>UTF-8param-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>encodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    springMVC-servlet.xml文件的修改

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <mvc:annotation-driven content-negotiation-manager="contentNegotiationManagerFactoryBean" />
        
        <context:component-scan base-package="ssm">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        context:component-scan>
    
        
        <mvc:default-servlet-handler/>
    
    
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            
            <property name="prefix" value="/WEB-INF/pages/"/>
            
            <property name="suffix" value=".jsp"/>
        bean>
    
    
    
    
        
        <bean id="contentNegotiationManagerFactoryBean" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
            <property name="favorPathExtension" value="false" />
            <property name="favorParameter" value="false" />
            <property name="ignoreAcceptHeader" value="false"/>
            <property name="mediaTypes">
                <map>
                    <entry key="json" value="application/json" />
                map>
            property>
        bean>
    beans>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    spring-config.xml文件的修改

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        
        <context:component-scan base-package="ssm">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        context:component-scan>
    
    
    
    
        
        <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true" />
        
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            
            <property name="typeAliasesPackage" value="ssm.pojo"/>
            
            <property name="dataSource" ref="dataSource"/>
            
            <property name="mapperLocations" value="classpath:ssm/dao/*.xml" />
    
        bean>
    
    
    
        
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        bean>
    
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="add*" isolation="REPEATABLE_READ" rollback-for="Exception"/>
                <tx:method name="update*" isolation="REPEATABLE_READ" rollback-for="Exception"/>
                <tx:method name="delete*" isolation="REPEATABLE_READ" rollback-for="Exception"/>
                <tx:method name="get*" isolation="REPEATABLE_READ" read-only="true"/>
                <tx:method name="*" isolation="REPEATABLE_READ" propagation="NOT_SUPPORTED"/>
    
            tx:attributes>
        tx:advice>
    
        
        <aop:config>
            <aop:pointcut expression="execution(* ssm.service..*(..))" id="point" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="point" />
        aop:config>
    
        
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="ssm.dao"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    添加数据库配置文件jdbc.properties

    
    
    jdbc.driver=com.mysql.jdbc.Driver
    
    jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8
    
    jdbc.username=root
    
    jdbc.password=root
    
    activiti.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    添加数据库表
    在这里插入图片描述

    CREATE TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(200) NOT NULL DEFAULT '',
      `sex` tinyint(1) DEFAULT '1',
      `age` int(11) NOT NULL DEFAULT '0',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    添加User.java

    package ssm.pojo;
    
    public class User {
    
        private Integer id;
    
        private String name;
    
        private Integer sex;
    
        private Integer age;
    
        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 Integer getSex() {
            return sex;
        }
    
        public void setSex(Integer sex) {
            this.sex = sex;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    添加UserDao.java

    package ssm.dao;
    
    import ssm.pojo.User;
    
    import java.util.List;
    
    public interface UserDao {
        List<User> getUserList();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    添加UserMapper.xml

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        <mapper namespace="ssm.dao.UserDao">
        <resultMap id="userMap" type="User">
            <id property="id" column="ID" />
            <result property="name" column="name" />
            <result property="sex" column="sex" />
            <result property="age" column="age" />
        resultMap>
        <select id="getUserList" resultMap="userMap">
            select * from user
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    添加UserService.java

    package ssm.service;
    
    import ssm.pojo.User;
    
    import java.util.List;
    
    public interface UserService {
        List<User> getUserList();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    添加UserServiceImpl.java

    package ssm.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import ssm.dao.UserDao;
    import ssm.pojo.User;
    import ssm.service.UserService;
    
    import java.util.List;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserDao userDao;
        public List<User> getUserList() {
            return userDao.getUserList();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    添加控制器Index.Controller

    package ssm.controller;
    
    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 ssm.dao.UserDao;
    import ssm.pojo.User;
    import ssm.service.UserService;
    
    import java.util.List;
    
    @Controller
    public class IndexController {
    
        @RequestMapping("index.html")
        public String index(){
            return "index";
        }
    
        @Autowired
        private UserService userService;
    
        @RequestMapping(path = "index.html",params ="act=list")
        @ResponseBody
        public List<User> users(){
            return  userService.getUserList();
        }
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    添加视图页面index.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" type="text/css" href="easyui/css/themes/default/easyui.css" />
        <link rel="stylesheet" type="text/css" href="easyui/css/themes/icon.css" />
        <link rel="stylesheet" type="text/css" href="easyui/css/common.css" />
        <script type="text/javascript" src="easyui/js/jquery-1.11.2.min.js"></script>
        <script type="text/javascript" src="easyui/js/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="easyui/js/easyui-lang-zh_CN.js"></script>
    </head>
    <body class="easyui-layout">
       Hello,world
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    项目结构如下
    在这里插入图片描述

    启动服务器可以看到查询数据库返回下面的结果
    在这里插入图片描述
    下载源码

  • 相关阅读:
    安全基础~通用漏洞5
    算法设计 (分治法应用实验报告)基于分治法的合并排序、快速排序、最近对问题
    五年制专转本备考中如何进行有效的自我管理
    C++学习之引用
    ELK创建仪表盘
    MySQL锁学习笔记
    一致性hash算法原理及实践
    【源码+名师讲解】Java游戏开发_Java飞机大战1.0进阶版_Java28个功能点能力提升必备_Java初级项目_Java练手项目_Java课程设计
    行业追踪,2023-09-26
    JUC笔记(二) --- Java线程
  • 原文地址:https://blog.csdn.net/chendongpu/article/details/126559938