2021-05-26 springWeb项目所需文件,后面慢慢补充

DAO层

非配置文件:

1.mybatis动态代理接口

xxxmapper.java文件

2.mybatis动态接口的映射文件

xxxmapper.xml文件

3.数据库中的表

4.实体类

表对应的类

配置文件:

1.mybatis主配置文件,这个文件空的,因为跟spring整合,所以在spring中配置,不过要保存一个空的文件,要不然会报错

SqlMapConfig.xml
<?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>
</configuration>

2.spring层配置文件

applicationContext-dao.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">


    <!--    配置jdbc属性文件扫描器-->
    <context:property-placeholder location="classpath:jdbc/jdbc.properties"/>

    <!--    配置一个数据库连接的对象 对象名字为dataSource,
        这个就是德鲁伊包给的一个类,配置一个对象,所有连接数据库的属性都在里面设置,
        然后可以用这个对象去连接数据库-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close" lazy-init="false">

        <!--    从属性文件获取连接数据库的参数-->
        <!--        其实就是给这个对象的属性赋值-->
        <property name="driverClassName" value="${jdbc.drive}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>

        <!--        获取连接池的参数-->
        <!--        继续赋值,连接池属性的赋值-->
        <property name="initialSize" value="${initialSize}"/>
        <property name="minIdle" value="${minIdle}"/>
        <property name="maxActive" value="${maxActive}"/>
        <property name="maxWait" value="${maxWait}"/>
    </bean>

        <!--    创建一个工厂类的对象,来通过上面的数据库连接驱动操作数据库
                这个本来是mybatis 的工作,用spring配置更方便一点-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--    配置这个对象的属性    -->
        <!--    提供访问数据库的数据源-->
        <property name="dataSource" ref="dataSource"/>

        <!--    配置mybatis的核心主配置文件-->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>

        <!--    配置mybatis的别名包扫描器,这个用了之后.在mapper文件里面就可以使用别名,返回值可以使用,方便-->
        <property name="typeAliasesPackage" value="com.xxxx.pojo"/>
    </bean>

    <!--    配置mybatis的动态代理及映射位置-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--        这是需要映射的包的位置-->
        <property name="basePackage" value="com.xxxx.mapper"/>
    </bean>
</beans>

3:JDBC属性文件

jdbc.properties

jdbc.drive = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mybd1?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8
jdbc.user = root
jdbc.password = root

## 设置连接池的参数
## 初始连接数
initialSize=10
## 最小空闲数
minIdle=10
## 最大连接数
maxActive=30
## 最大的等待时间
maxWait=6000

Service层

非配置文件

1.业务接口类

xxxservice.java

2.业务接口实现类

xxxserviceimpl.java

配置文件

applicationContext-service.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: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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--    导入dao层文件,可以使用dataSource来陪着事务管理器-->
    <import resource="classpath:spring/applicationContext-dao.xml"/>

    <!--    使用注解开发的包扫描器-->
    <context:component-scan base-package="com.bjpwnd.service"/>
<!--事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

<!--    这个可以用注解来表明事务-->
    <tx:annotation-driven transaction-manager="transactionManager"/>


</beans>

使用注解管理jdbc事务

@Transactional(isolation = Isolation.SERIALIZABLE,propagation = Propagation.REQUIRED)
    public boolean transfer(int out, int in, int money) {
        int money1 = -money;
        int i = accountMapper.changeMoney(out, money1);
//        int i1 = 10 / 0;
        int j = accountMapper.changeMoney(in, money);
        return i > 0 && j > 0;
    }

Controller层

非配置文件

1.控制器类

xxxcontroller.java

配置文件

springmvc.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/beans
       http://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">

    <!--    使用注解开发的包扫描器-->
    <context:component-scan base-package="com.bjpwnd.controller"/>
    <!--配置处理器映射器-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->

    <!--配置处理器适配器-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->

<!--    使用默认的servlet来响应静态文件,如果在web.xml中定义了路径为 '/',会导致不能访问
         静态资源,比如说加载图片之类的,如果配置了.xx就不会-->
    <mvc:default-servlet-handler/>
<!--    这个效果等同于上面 location表示路径   mapping表示匹配的文件-->
<!--                    /xx/              /**表示所有文件-->
<!--    <mvc:resources mapping="" location=""/>-->

<!--可以配合@ResponseBody使用,还可以直接返回json对象   -->
    <mvc:annotation-driven/>

<!--    视图解析器配置:视图解析器 可能相当于之前的 servlet的增强-->
    <bean id="internalResourceViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">


    
<!--    初始化spring容器文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
<!--    用监听器加载所有的spring容器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    
<!--配置servlet映射,跟之前的servlet差不多 不过多了一个init-param-->
    <servlet>
        <servlet-name>Controller</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--        容器配置文件路径-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
<!--会解析所有以.go结尾的访问-->
    <servlet-mapping>
        <servlet-name>Controller</servlet-name>
        <url-pattern>*.go</url-pattern>
    </servlet-mapping>


<!--    过滤器,改编码用-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

项目文件结构

image.png

配置文件能复制就复制,别自己写,多看看就行!

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

推荐阅读更多精彩内容

  • 备课作文,想到考试的时候,我们基本不会出现写风景这类的题目,往往是考察情感认识的题目。而我们生活的狭窄范围,学校和...
    浅浅慢慢阅读 211评论 1 3
  • 备考真的太煎熬了。 真的想知道那些前辈是怎么熬过少则几个月,多则几年的备考时间。 从12号备考到现在才两周,已经觉...
    豆爷的铲屎官阅读 119评论 0 0
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 125,044评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,050评论 0 4