maven+spring+mybatis配置3步走

maven+spring+mybatis配置第1步:配置pom.xm依赖

1.配置slfj+logback:

Java代码  收藏代码
    <!-- Logging -->  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-api</artifactId>  
                <version>${org.slf4j.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>jcl-over-slf4j</artifactId>  
                <version>${org.slf4j.version}</version>  
                <scope>runtime</scope>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-log4j12</artifactId>  
                <version>${org.slf4j.version}</version>  
                <scope>runtime</scope>  
            </dependency>  
            <dependency>  
                <groupId>log4j</groupId>  
                <artifactId>log4j</artifactId>  
                <version>1.2.15</version>  
                <exclusions>  
                    <exclusion>  
                        <groupId>javax.mail</groupId>  
                        <artifactId>mail</artifactId>  
                    </exclusion>  
                    <exclusion>  
                        <groupId>javax.jms</groupId>  
                        <artifactId>jms</artifactId>  
                    </exclusion>  
                    <exclusion>  
                        <groupId>com.sun.jdmk</groupId>  
                        <artifactId>jmxtools</artifactId>  
                    </exclusion>  
                    <exclusion>  
                        <groupId>com.sun.jmx</groupId>  
                        <artifactId>jmxri</artifactId>  
                    </exclusion>  
                </exclusions>  
                <scope>runtime</scope>  
            </dependency>  
            <dependency>  
                <groupId>ch.qos.logback</groupId>  
                <artifactId>logback-classic</artifactId>  
                <version>${logback.version}</version>  
                <type>jar</type>  
                <scope>compile</scope>  
            </dependency>  
            <dependency>  
                <groupId>ch.qos.logback</groupId>  
                <artifactId>logback-core</artifactId>  
                <version>${logback.version}</version>  
                <type>jar</type>  
                <scope>compile</scope>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>jcl-over-slf4j</artifactId>  
                <version>${org.slf4j.version}</version>  
                <type>jar</type>  
                <scope>compile</scope>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>log4j-over-slf4j</artifactId>  
                <version>${org.slf4j.version}</version>  
                <type>jar</type>  
                <scope>compile</scope>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>jul-to-slf4j</artifactId>  
                <version>${org.slf4j.version}</version>  
                <type>jar</type>  
                <scope>compile</scope>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-api</artifactId>  
                <version>${org.slf4j.version}</version>  
                <type>jar</type>  
                <scope>compile</scope>  
            </dependency>  




2.Spring配置:
Java代码  收藏代码
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>${org.springframework.version}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>${org.springframework.version}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency> 


3.Mybatis配置:

另外还需要给mybatis加一个repository:

4.BoneCP数据源的依赖

还需要给BoneCP加一个repostory:
Java代码  收藏代码
        <repository>  
            <releases>  
                <enabled>true</enabled>  
            </releases>  
            <id>bonecp-repo</id>  
            <name>BoneCP Repository</name>  
            <url>http://jolbox.com/bonecp/downloads/maven</url>  
        </repository> 


maven+spring+mybatis配置第2步:配置spring


1.配置Datasource:

2.配置Mybatis:

3.配置Mybatis:

这里需要AspectJ的Jar包,比较恶心的是aspectj没有maven支持,所以可以把AspectJ相关的jar包拷贝到war包里的lib下。


maven+spring+mybatis配置第3步:生成Mybatis Bean


1.配置pom.xml
给pom.xml加Mybatis的plugin:


还需要Mybatis的repository,在前面已经加过了。另外,需要加一个Mybatis generator的配置文件名的property:

2.配置generatorConfig.xml
Java代码  收藏代码
    <?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>  
        <classPathEntry location=".\\src\\main\\webapp\\WEB-INF\\lib\\classes12.jar" />  
      
        <context id="DB2Tables" targetRuntime="MyBatis3">  
            <jdbcConnection driverClass="JDBC驱动名"  
                connectionURL="jdbc:oracle:thin:@192.168.122.241:1521:orcl" userId="user"  
                password="password">  
            </jdbcConnection>  
      
            <javaTypeResolver>  
                <property name="forceBigDecimals" value="false" />  
            </javaTypeResolver>  
      
            <javaModelGenerator targetPackage="com.rever.mybatis.model"  
                targetProject=".\src\main\java">  
                <property name="enableSubPackages" value="true" />  
                <property name="trimStrings" value="true" />  
            </javaModelGenerator>  
      
            <sqlMapGenerator targetPackage="com.rever.mybatis.model"  
                targetProject=".\src\main\resources">  
                <property name="enableSubPackages" value="true" />  
            </sqlMapGenerator>  
      
            <javaClientGenerator type="XMLMAPPER"  
                targetPackage="com.rever.mybatis.model" targetProject=".\src\main\java">  
                <property name="enableSubPackages" value="true" />  
            </javaClientGenerator>  
      
            <table tableName="TableName" domainObjectName="TableNameObject">  
                <generatedKey column="ID" sqlStatement="select s_sequence.nextval from dual"  
                    type="pre" />  
            </table>  
        </context>  
    </generatorConfiguration> 



这里需要注意一点的是生成的xml存放路径最好放在src\main\resources路径下,否则Maven打包的时候不会把相关的xml文件打到war包里的。
3.执行maven命令,生成Mybatis文件:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值