SpringMVC环境搭建--Birber

利用gradle+SpringMVC+Mybatis搭建环境
首先引入需要的用到的jar包(在gradle中直接添加依赖即可)

    compile group: 'org.springframework', name: 'spring-core', version: '4.3.14.RELEASE'

    compile group: 'org.springframework', name: 'spring-web', version: '4.3.14.RELEASE'

    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.14.RELEASE'

    compile group: 'org.springframework', name: 'spring-context', version: '4.3.14.RELEASE'

    compile group: 'org.springframework', name: 'spring-beans', version: '4.3.14.RELEASE'

    compile group: 'commons-logging', name: 'commons-logging', version: '1.1.1'

    compile group: 'org.springframework', name: 'spring-aspects', version: '5.0.7.RELEASE'

    compile group: 'org.springframework', name: 'spring-expression', version: '5.0.8.RELEASE'

    compile group: 'org.springframework', name: 'spring-aop', version: '5.0.8.RELEASE'

    compile group: 'javax.servlet', name: 'jstl', version: '1.2'

第二步写web.xml文件
这里写图片描述
如上图所示,在webapp文件夹下新建WEB-INF文件夹,然后在WEB-INF文件夹下新建web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>


第三步,在resources下新建对应名称的Spring-servelt.xml文件,
我这里对应的名称叫spring.xml,
这里写图片描述

<?xml version="1.0" encoding="UTF-8"?>
<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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com"></context:component-scan>

    <!-- 加载静态资源 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:default-servlet-handler />

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

写到这里我们就已经完成了SpringMVC的环境搭建了,我们来写个controller看看,
!注意文件存放位置

这里写图片描述
这里写图片描述


接下来,我们看看怎么使用mybatisGenerator自动生成文件

首先,在resources文件夹下新建mybatis文件夹,在里面新建jdbc.properties和generatorConfig.xml文件

jdbc.properties:
请根据自己数据库实际情况更改用户名、密码、数据库名称、以及生成文件的包名

# JDBC 驱动类名
jdbc.driverClassName=com.mysql.jdbc.Driver
# JDBC URL: jdbc:mysql:// + 数据库主机地址 + 端口号 + 数据库名
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=UTC
# JDBC 用户名及密码
jdbc.username=root
jdbc.password=root

# 生成实体类所在的包
package.model=com.djb.model
# 生成 mapper 类所在的包
package.mapper=com.djb.dao
# 生成 mapper xml 文件所在的包,默认存储在 resources 目录下
package.xml=mapper

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <context id="MysqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <commentGenerator>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}"
                        userId="${userId}"
                        password="${password}">
        </jdbcConnection>
        <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}"/>
        <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}"/>
        <javaClientGenerator targetPackage="${mapperPackage}" targetProject="${src_main_java}" type="XMLMAPPER"/>
        <!-- sql占位符,表示所有的表 -->

        <table tableName="%" enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">

        </table>

    </context>
</generatorConfiguration>

然后,在 gradle 的 dependencies 中添加需要的三个依赖

    mybatisGenerator 'org.mybatis.generator:mybatis-generator-core:1.3.5'
    mybatisGenerator 'mysql:mysql-connector-java:5.1.40'
    mybatisGenerator 'tk.mybatis:mapper:3.3.9

添加一个configurations

configurations {
    mybatisGenerator
}

最后加上task

def getDbProperties = {
    def properties = new Properties()
    file("src/main/resources/mybatis/jdbc.properties").withInputStream { inputStream ->
        properties.load(inputStream)
    }
    properties;
}
task mybatisGenerate << {
    def properties = getDbProperties()
    ant.properties['targetProject'] = projectDir.path
    ant.properties['driverClass'] = properties.getProperty("jdbc.driverClassName")
    ant.properties['connectionURL'] = properties.getProperty("jdbc.url")
    ant.properties['userId'] = properties.getProperty("jdbc.username")
    ant.properties['password'] = properties.getProperty("jdbc.password")
    ant.properties['src_main_java'] = sourceSets.main.java.srcDirs[0].path
    ant.properties['src_main_resources'] = sourceSets.main.resources.srcDirs[0].path
    ant.properties['modelPackage'] = properties.getProperty("package.model")
    ant.properties['mapperPackage'] = properties.getProperty("package.mapper")
    ant.properties['sqlMapperPackage'] =properties.getProperty("package.xml")
    ant.taskdef(
            name: 'mbgenerator',
            classname: 'org.mybatis.generator.ant.GeneratorAntTask',
            classpath: configurations.mybatisGenerator.asPath
    )
    ant.mbgenerator(overwrite: true,
            configfile: 'src/main/resources/mybatis/generatorConfig.xml', verbose: true) {
        propertyset {
            propertyref(name: 'targetProject')
            propertyref(name: 'userId')
            propertyref(name: 'driverClass')
            propertyref(name: 'connectionURL')
            propertyref(name: 'password')
            propertyref(name: 'src_main_java')
            propertyref(name: 'src_main_resources')
            propertyref(name: 'modelPackage')
            propertyref(name: 'mapperPackage')
            propertyref(name: 'sqlMapperPackage')
        }
    }
}

最后运行 mybatisGenerate
这里写图片描述

如出现以下情况,请检查jdbc.properties的属性名是否与task里的一致
这里写图片描述



如出现以下情况,请检查 generatorConfig.xml 中 jdbcConnection 的代码是否正确
这里写图片描述
如不相同请把generatorConfig.xml 中 jdbcConnection 的代码改为


<jdbcConnection driverClass="driverClass"
connectionURL="${connectionURL}"
userId="${userId}"
password="${password}">
</jdbcConnection>
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值