JavaWeb:dao层

dao层是数据访问层,我们使用的是mybatis框架,所以我们需要添加mybatis相关依赖,以及数据库连接池依赖;
JavaWeb:dao层_第1张图片

  • 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">
    <parent>
        <artifactId>###dao层模块名称###artifactId>
        <groupId>###groupId名称###groupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <artifactId>###dao层模块名称###artifactId>

    <dependencies>

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

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

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>###mysql版本###version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.0.9version>
        dependency>

    dependencies>

    <build>
        <resources>
            
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
    build>
project>
  • 配置数据库连接信息
    在src/main/resources/目录下新建一个 spring 文件夹,然后新建 dataSource.properties 文件,配置数据库连接信息
#驱动类
driverClassName = com.mysql.jdbc.Driver
#数据库url
mysql.url = jdbc:mysql://localhost:3306/###使用的数据库名称###?useSSL=false
#账户名和密码
mysql.userName = root
mysql.password = 123456
  • 配置spring上下文
    在spring目录下,新建一个 applicationContext-dao.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:p="http://www.springframework.org/schema/p"
       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/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    
    <context:component-scan base-package="cn.edu.zzti.cs.dao.**"/>

    
    <context:property-placeholder location="classpath:/spring/dataSource.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"
          p:driverClassName="${driverClassName}"
          p:url="${mysql.url}"
          p:username="${mysql.userName}"
          p:password="${mysql.password}"/>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="mapperLocations" value="classpath:mappers/**/*.xml" />
        
        
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.edu.zzti.cs.dao" />
    bean>

    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <tx:annotation-driven/>

beans>
  • mybatis配置
    我们可以使用一个xml配置文件去设置mybatis的一些 configuration ,在src/main/resources/目录下新建一个mybatis 文件夹,然后新建一个 mybatis-config.xml 文件


<configuration>
    <settings>
        
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    settings>
configuration>
  • 日志配置
  1. 在resources文件夹下新建一个log4j.properties
log4j.rootLogger=DEBUG,stdout
log4j.logger.com.ibatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} %m%n
  1. 在resources文件夹下新建一个log4j2.xml

<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        Console>
    Appenders>
    <Loggers>
        <logger name="org.springframework" level="WARN" />
        <logger name="org.mybatis" level="WARN" />
        <logger name="cn.edu.zzti.cs" level="DEBUG"/>
        <Root level="INFO">
            <AppenderRef ref="Console" />
        Root>
    Loggers>
Configuration>
  • Sql语句
  1. 写在谁mapper.xml

通过sql语句标签的id来绑定接口的函数
JavaWeb:dao层_第2张图片
2. 直接以注释的形式写在接口里(简单sql)

JavaWeb:dao层_第3张图片

你可能感兴趣的:(JavaEEstudy)