我快吐了——使用maven配置SSM(就是想固执的使用注解开发)(二))

使用maven配置SSM项目步骤

1. 创建maven的web项目,并规范其文件夹结构,保留其web.xml文件

2. 配置pom的项目所需依赖(按照jar包的需求进行配置)

3. 配置SpringMVC的配置文件


<?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:mvc="http://www.springframework.org/schema/mvc"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

      http://www.springframework.org/schema/mvc

      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

      http://www.springframework.org/schema/context

      http://www.springframework.org/schema/context/spring-context-3.2.xsd

      http://www.springframework.org/schema/aop

      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

      http://www.springframework.org/schema/tx

      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 1.配置注解扫描位置 -->

    <context:component-scan base-package="com.gyf.backoffice.web.controller" />

    <!-- 2.配置注解处理映射-->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <!--3.配置适配器-->

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"

    </bean>

    <!-- 4.配置springmvc视图解析器 视图解析器解析的视频路径为:前缀 + 后缀 -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/views" />

        <property name="suffix" value=".jsp" />

    </bean>

  </beans>


ps:规范化操作应该将配置文件都放入到src/main下的资源文件夹resources文件夹下,但是这里的配置改变了idea默认读取webapp/WEB-INF/DispatcherServlet-servlet.xml,更换配置文件的默认路径


<!--配置springmvc-->

<servlet>

    <servlet-name>DispatcherServlet</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!-- 3.0的springmvc 默认加载WEB-INF下的dispatcher-servlet.xml文件 3.2的springmvc

        加载DispatcherServlet-servlet.xml文件 -->

    <init-param>

        <!-- 修改黑底springmvc加载的配置文件路径 -->

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath:spring/springmvc.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

    <servlet-name>DispatcherServlet</servlet-name>

    <url-pattern>*.do</url-pattern>

</servlet-mapping>


这里会遇到问题——找不到springmvc.xml的文件的错误,但是明明classpath:spring/springmvc.xml文件的了路径正确

问题1:更改默认springmvc的配置文件但是,idea找不到的问题?

原因是:idea对文件的打包编码不能将resources文件的配置文件打包

解决方案,增加配置文件的打包路径

https://blog.csdn.net/sinat_38301574/article/details/80465693

4. 然后配置Controller进行测试,是否可以访问到servlet(最好使用注解访问,注解方便快捷,可以有更少的配置)


5. 使用MyBatis的逆向工程生成JavaBean/Mapper(可以方便快捷的构造出mybatis所需要的文件)



<?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="mysqlTable" targetRuntime="MyBatis3">

        <!-- 1.数据连接参数 -->

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"

                        connectionURL="jdbc:mysql://localhost:3306/shopping?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT%2B8&amp;useSSL=false&amp;allowPublicKeyRetrieval=true"

                        userId="root"

                        password="">

        </jdbcConnection>

        <!-- 2.默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL

                和 NUMERIC 类型解析为java.math.BigDecimal -->

        <javaTypeResolver >

            <property name="forceBigDecimals" value="false" />

        </javaTypeResolver>

        <!-- 3.生成模型的位置 -->

        <javaModelGenerator targetPackage="com.rsw.modal" targetProject=".\src">

            <!-- enableSubPackages:是否让schema作为包的后缀 -->

            <property name="enableSubPackages" value="true" />

            <property name="trimStrings" value="true" />

        </javaModelGenerator>

        <!-- 4.targetProject:mapper映射文件生成的位置 -->

        <sqlMapGenerator targetPackage="com.rsw.mapper"  targetProject=".\src">

            <!-- enableSubPackages:是否让schema作为包的后缀 -->

            <property name="enableSubPackages" value="true" />

        </sqlMapGenerator>

        <!-- 5. targetPackage:mapper接口生成的位置 -->

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.rsw.mapper"

                            targetProject=".\src">

            <property name="enableSubPackages" value="true" />

        </javaClientGenerator>

        <!-- 6.要生成的表,及生成的类名,类名首字母大写(自动) -->

        <table tableName="administrator" />

        <table tableName="cart"/>

        <table tableName="category"/>

        <table tableName="category_second" domainObjectName="categorySecond" />

        <table tableName="item"/>

        <table tableName="orderform"/>

        <table tableName="orderitem"/>

        <table tableName="user"/>

    </context>

</generatorConfiguration>





public static void main(String[] args)  throws Exception{

    List<String> warnings = new ArrayList<String>();

    boolean overwrite = true;

    File configFile = new File("src/generatorConfig.xml");

    ConfigurationParser cp = new ConfigurationParser(warnings);

    Configuration config = cp.parseConfiguration(configFile);

    DefaultShellCallback callback = new DefaultShellCallback(overwrite);

    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,

            callback, warnings);

    myBatisGenerator.generate(null);

}



6. 测试Mapper.java和Mapper.xml中的数据库方法是否可以执行成功


7. 定义service层


8. 配置mybatis的配置文件



<?xml version="1.0"

encoding="UTF-8"

?>

<!DOCTYPE configuration

PUBLIC "-//mybatis.org//DTD

Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

     <!-- 别名配置 -->

     <typeAliases>

          <!-- 批量配置别名:指定批量定义别名的类包,别名为类名(首字母大小写都可) -->

          <package name="com.gyf.backoffice.domain"/>

     </typeAliases>

     <mappers>

          <!-- 批量加载映射文件 -->

          <package name="com.gyf.backoffice.mapper"/>

     </mappers>

</configuration>



ps:这里会出现另一个问题,找不到类com.moder.User

问题2:找不到moder层的表的映射文件

因为配置的别名,mybatis逆向工程里面的type的类型一般还是具体路径所以冲突

9.配置spring的配置文件applicaiontContext.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:aop="http://www.springframework.org/schema/aop"

      xmlns:tx="http://www.springframework.org/schema/tx"

      xsi:schemaLocation="http://www.springframework.org/schema/beans

      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

      http://www.springframework.org/schema/context

      http://www.springframework.org/schema/context/spring-context-3.2.xsd

      http://www.springframework.org/schema/aop

      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

      http://www.springframework.org/schema/tx

      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!--开启注解-->

    <context:annotation-config/>

    <!-- 配置扫描注解 -->

    <context:component-scan base-package="com.rsw"/>

    <!-- 1.加载db配置文件  -->

    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 2.配置c3p0数据源 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <property name="driverClass" value="${driverClass}"/>

        <property name="jdbcUrl" value="${jdbcUrl}"/>

        <property name="user" value="${user}"/>

        <property name="password" value="${password}"/>

        <property name="initialPoolSize" value="${jdbc.initPoolSize}"/>

        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>

    </bean>

    <!-- 3.让spring管理sqlsessionFactory -->

    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource"/>

        <!-- 指定配置文件位置 -->

        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>

    </bean>

    <!-- 4.配置mapper扫描器.批量扫描创建代理对象 -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="basePackage" value="com.rsw.mapper"/>

        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>

    </bean>

    <!-- 5.配置事务管理器 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>

    </bean>

    <!-- 6.开启事务注解-->

    <tx:annotation-driven/>

</beans>



ps:这里会遇道超级恶心的问题:

注解使用不上,不使用注解的话就不会报错

jdk1.8使用注解的话,要使用spring的高版本才行,要不会报错

问题:注解使用失败

解决:

<!-- 配置扫描注解 -->

    <context:component-scan base-package="com.rsw"/>

配置注解的注解扫描要精确到具体的包,要不就会报错,而且是各种莫名其妙的错误,需要在pox.xml配置各种依赖环境,到最后才能找到问题是注解扫描没有配置到具体的包

在maven项目下的项目结构src/mian/java下最好在来一层包,比如“com.xxxx”,这样的话就可以配置一次就可以使用。“src/mian/java”的“java”并不算是包,所以不能配置。

10. 之后就可以进行项目开发了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355