配置文件整合

以下都是我日常会使用到的配置文件模板

文章目录

    • mybatis
      • `mybatis-config.xml`
      • `db.propertis`
      • `mapper.xml`
      • `MybatisUtil`
    • spring
      • `applicationContext.xml`
    • web
      • `web.xml`
    • 部分maven依赖
      • `junit`
      • `mysql-jdbc`
      • `mybatis`
      • `spring-jdbc`
      • `aspectjweaver`
      • `mybatis-spring`
      • `lombok`
    • 资源导出过滤问题
      • maven中添加以下配置

mybatis

mybatis-config.xml


DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <properties resource="db.properties">
        <property name="username" value="root"/>
    properties>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    settings>
    
    <typeAliases>
        <package name="com.wq.pojo"/>
    typeAliases>
<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        dataSource>
    environment>
environments>

    <mappers>
        <mapper resource="com/wq/dao/BlogMapper.xml"/>
    mappers>
configuration>

随之而来的就会有一个

db.propertis

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mybatis?serverTimezone=GMT&allowPublicKeyRetrieval=true&useSSL=false&characterEncoding=utf8
username=root
password=wang12345

mapper.xml


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.wq.mapper.UserMapper">
    <select id="queryUser" resultType="user">
        select * from user
    select>
mapper>

获取sqlSession的工具类

MybatisUtil

package com.wq.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {
    static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            //使用mybatis的第一步 获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
    //SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
    //你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

spring

applicationContext.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
		https://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context
		https://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="com"/>
    <context:annotation-config/>

    <bean id="userService" class="com.wq.service.UserServiceImpl"/>
    
    <aop:aspectj-autoproxy/>
beans>

web

web.xml



<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
                      https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
         version="6.0"
         metadata-complete="true">


  <servlet>
    <servlet-name>helloservlet-name>
    <servlet-class>com.wq.servlet.HelloServletservlet-class>
  servlet>
  <servlet>
    <servlet-name>errorservlet-name>
    <servlet-class>com.wq.servlet.ErrorServletservlet-class>
  servlet>

  <servlet-mapping>
    <servlet-name>helloservlet-name>
    <url-pattern>/hellourl-pattern>
  servlet-mapping>
  <servlet-mapping>
    <servlet-name>errorservlet-name>
    <url-pattern>/*url-pattern>
  servlet-mapping>

  




web-app>

部分maven依赖

junit

<dependency>
	<groupId>junitgroupId>
	<artifactId>junitartifactId>
    <version>4.13.2version>
dependency>

mysql-jdbc

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.33version>
        dependency>

mybatis

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.13version>
        dependency>

spring-jdbc

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>6.0.11version>
        dependency>

aspectjweaver

        
        
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.9.19version>
        dependency>

mybatis-spring

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>3.0.2version>
        dependency>

lombok

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.24version>
        dependency>

资源导出过滤问题

maven中添加以下配置

    <build>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>
    build>

你可能感兴趣的:(笔记)