Mybatis 复习 Mybatis 配置 Mybatis项目结构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis 复习 Mybatis 配置 Mybatis项目结构相关的知识,希望对你有一定的参考价值。

项目结构:

技术分享

我在applicationContext.xml中有两个跟mybatis有关的内容:

<import resource="mybatis-config.xml"/>    这里面配置了mybatis自身的配置信息

<import resource="persistence-config.xml"/>    这里面配置了数据源信息

applicationContext.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
                        http://www.springframework.org/schema/context/spring-context.xsd
                        ">
    <import resource="mybatis-config.xml"/>
    <import resource="persistence-config.xml"/>
    <import resource="quartz-config.xml"/>
    <import resource="batch-config/springbatch-config.xml"/>
    <context:component-scan base-package="com.blog.controller,com.blog.dao,com.blog.bean,com.blog.service" />
</beans>

而mybatis-config.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>
    <settings>
        <setting name="lazyLoadingEnabled" value="false" />
    </settings>
    <!-- <typeAliases>
        <typeAlias alias="article" type="com.blog.bean.Article" />
    </typeAliases> -->
</configuration>

persistence-config.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:aop="http://www.springframework.org/schema/aop"
       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/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                              ">
    <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean> -->
    <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- mapper和resultmap配置路径 -->
        <property name="mapperLocations">
            <list>
                <!-- 表示在以下所有目录中,扫描以mapper.xml结尾所有文件 -->
                <value>classpath:mapper/*mapper.xml</value>
            </list>
        </property>
    </bean>
    
    <!-- 通过扫描的模式,扫描目录在com/blog/interfces目录下,所有的mapper都继承SqlMapper接口的接口, 这样一个bean就可以了 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.blog.interfaces" />
        <property name="markerInterface" value="com.blog.interfaces.SqlMapper" />
    </bean>
    
    <!-- 事务管理器配置,单数据源事务 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 配置那些类的方法进行事务管理 -->
    <aop:config>
        <aop:pointcut id="executeService"
            expression="execution(* com.lavasoft.*.service..*(..))" />
        <aop:advisor pointcut-ref="executeService" advice-ref="txAdvice" />
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="load*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="query*" read-only="true" />
            <tx:method name="read*" read-only="true" />
            <tx:method name="sync*" />
            <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
        </tx:attributes>
    </tx:advice>
</beans>

注意上面的: <!-- 通过扫描的模式,扫描目录在com/blog/interfaces目录下,所有的mapper都继承SqlMapper接口的接口, 这样一个bean就可以了 -->

附jdbc.properties:

#oracle
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521/****
jdbc.username=****
jdbc.password=****

#mysql
#jdbc.driverClassName=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/****
#jdbc.username=****
#jdbc.password=****

而com.blog.interfaces包里,继承了SqlMapper的接口为CoreMapper:

public interface CoreMapper extends SqlMapper{
    public List<String> getArticleTitleId();
    public Article getArticleNewest();
    public Article getArticleById(int articleid);
    public void addArticle(Article article);
    public void updateArticle(Article article);
    public void deleteArticle(int articleid);
    public String getPasswordByUserName(String username);
    public void updatepassword(String password);
    public List<Passport> getPassport();
}

SqlMapper的内容很简单:

package com.blog.interfaces;
/**
* 所有的Mapper继承这个接口
*/
public interface SqlMapper {
    
}

 

至此,主要的配置都结束了,剩下的就是SQL语句的配置了coremapper.xml(该文件放在哪儿,配置在persistence-config里了):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace和定义的Mapper接口对应,并实现其中的方法 -->
<mapper namespace="com.blog.interfaces.CoreMapper">
    
    <select id="getArticleTitleId" resultType="com.blog.bean.Article">
        select articleid,articletitle from t_article where status != ‘IACT‘
    </select>
    
    <select id="getArticleNewest" resultType="com.blog.bean.Article">
        select * from t_article where datelastmaint = (select max(datelastmaint) from t_article where status != ‘IACT‘)
    </select>
    
    <select id="getArticleById" parameterType="int" resultType="com.blog.bean.Article">
        select * from t_article where articleid=#{articleid}
    </select>
    
    <update id="updateArticle" parameterType="com.blog.bean.Article">
        update t_article set status = ‘ACT‘,articletitle=#{articletitle},content=#{content},datelastmaint=#{datelastmaint} where articleid=#{articleid}
    </update>
    
    <update id="deleteArticle" parameterType="int">
        update t_article set status = ‘IACT‘ where articleid=#{articleid}
    </update>
    
    <!-- MYSQL -->
    <!-- <insert id="addArticle" parameterType="com.blog.bean.Article">
    insert into t_article(articletitle,authorid,content,cover,datepublish,articlecatcd,status,datelastmaint) 
        values(#{articletitle},#{authorid},#{content},#{cover},#{datepublish},#{articlecatcd},#{status},#{datelastmaint})
    </insert> -->
    
    <!-- ORACLE -->
    
    <insert id="addArticle" parameterType="com.blog.bean.Article">
        insert into t_article(articleid,articletitle,authorid,content,datepublish,articlecatcd,datelastmaint) 
        values(s_article_pk.nextval,#{articletitle},#{authorid},#{content,jdbcType=CLOB},#{datepublish},#{articlecatcd},#{datelastmaint})
    </insert>
    
    
    <resultMap type="com.blog.bean.User" id="usermap">
        <id property="userid" column="userid"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
    </resultMap>
    
    <select id="getUserById" parameterType="string" resultMap="usermap">
        select * from t_user where userid = #{userid}
    </select>

    <!-- 根据用户名取得密码 -->
    <select id="getPasswordByUserName" parameterType="string" resultType="string">
        select password from t_password a where a.userid=(select b.userid from t_user b where b.username=#{username})
    </select>
    
    <!-- 密码生成器 获取密码 -->
    <select id="getPassport" parameterType="string" resultType="com.blog.bean.Passport">
        select * from t_passport
    </select>
    
</mapper>

 附: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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.wangx.blog</groupId>
    <artifactId>blog</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>blog Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>4.0.0.RELEASE</spring.version>
    </properties>
    <dependencies>
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context-support</artifactId>  
            <version>${spring.version}</version>
        </dependency>  
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>
        
        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- others -->

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.14</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.annotation</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.9</version>
        </dependency>

        <!-- logback 一个包足够 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1.6</version>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <!-- 文件上传 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        
        <!-- 添加oracle jdbc driver -->  
        <dependency>    
            <groupId>com.oracle</groupId>    
            <artifactId>ojdbc14</artifactId>    
            <version>10.2.0.3.0</version>
        </dependency>
        
        <!-- Spring Batch -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>3.0.0.RELEASE</version>
        </dependency>
        
        <!-- 调度器 -->
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>1.8.5</version>
        </dependency>
    </dependencies>
    
    <repositories>
        <repository>
            <!-- 好像没起到作用,后面再观察 -->
            <id>nexus-aliyun</id>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    
    <build>
        <finalName>blog</finalName>
        <pluginManagement>
            <plugins>  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-archetype-plugin</artifactId>  
                    <version>2.4</version>
                </plugin>
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-compiler-plugin</artifactId>  
                    <configuration>  
                        <source>1.7</source>  
                        <target>1.7</target>  
                    </configuration>  
                </plugin>  
                <plugin>  
                    <groupId>org.apache.maven.plugins</groupId>  
                    <artifactId>maven-resources-plugin</artifactId>  
                    <configuration>  
                        <encoding>UTF-8</encoding>  
                    </configuration>  
                </plugin>  
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.1-alpha-1</version>
                    <configuration>
                        <!--
                                                                             打包之前过滤掉不想要被打进 .war包的jar,注意:这个地方,本来路径应该是
                            WEB-INF/lib/anaalyzer-2.0.4.jar,但是经过多次试验,不能这样,至于咋回事儿,搞不清楚。。经多方查证均无结果
                                                                             暂且这样吧,虽然显得很丑陋,但是总能解决问题吧
                        -->
                        <warSourceExcludes>*/lib/analyzer-2.0.4.jar</warSourceExcludes>
                        <webResources>
                            <resource>
                                <!-- 元配置文件的目录,相对于pom.xml文件的路径 -->
                                <directory>src/main/webapp/WEB-INF</directory>
         
                                <!-- 是否过滤文件,也就是是否启动auto-config的功能 -->
                                <filtering>true</filtering>
         
                                <!-- 目标路径 -->
                                <targetPath>WEB-INF</targetPath>
                            </resource>
                        </webResources>
                    </configuration>
                </plugin>
            </plugins>  
        </pluginManagement>
    </build>
</project>

 

以上是关于Mybatis 复习 Mybatis 配置 Mybatis项目结构的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis配置文件――properties属性

关于框架Mybatis------浅谈基础知识

MyBatis简介与配置MyBatis+Spring+MySql

MyBatis_记录

MyBatis笔试题

第七天.spring boot 整合mybatis