IDEA+maven+mybatis+generator笔记

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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.lyjgroupId>
    <artifactId>MybatisTest1artifactId>
    <packaging>warpackaging>
    <version>1.0-SNAPSHOTversion>
    <name>MybatisTest1name>
    <url>http://maven.apache.orgurl>
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.39version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.1version>
        dependency>

    dependencies>
    <build>
        
        <plugins>
            <plugin>
                <groupId>org.mybatis.generatorgroupId>
                <artifactId>mybatis-generator-maven-pluginartifactId>
                <version>1.3.5version>
                <configuration>
                    <verbose>trueverbose>
                    <overwrite>trueoverwrite>
                configuration>
            plugin>
        plugins>
        
        <resources>  
            <resource>  
                <directory>src/main/javadirectory>  
                <includes>  
                    <include>**/*.xmlinclude>  
                includes>  
            resource>  
        resources>  
        <finalName>MybatisTest1finalName>
    build>
project>

2.编写generatorConfig.xml 自动生成mybatis映射文件的配置文件



<generatorConfiguration>
    <properties resource="db.properties"/>
    
    <classPathEntry location="${jdbc.driverLocation}" />
    <context id="default" targetRuntime="MyBatis3">
        <commentGenerator>
            
            <property name="suppressAllComments" value="true" />
        commentGenerator>
        
        <jdbcConnection driverClass="${jdbc.driverClass}"
                        connectionURL="${jdbc.jdbcUrl}" userId="${jdbc.user}"
                        password="${jdbc.password}">
        jdbcConnection>
        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="${jdbc.pojoLocation}"
                            targetProject="src/main/java">
            
            <property name="enableSubPackages" value="false" />
            
            <property name="trimStrings" value="true" />
        javaModelGenerator>
        
        <sqlMapGenerator targetPackage="${jdbc.mapperLocation}"
                         targetProject="src/main/java">
            
            <property name="enableSubPackages" value="false" />
        sqlMapGenerator>
        
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="${jdbc.mapperLocation}"
                             targetProject="src/main/java">
            
            <property name="enableSubPackages" value="false" />
        javaClientGenerator>
        
        
        <table schema="" tableName="user">table>
    context>
generatorConfiguration>

3.db.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///sssptest?useUnicode=true&characterEncoding=UTF-8
jdbc.user=lyj
jdbc.password=123


jdbc.driverLocation=E:/JAVA/maven/mysql/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar

jdbc.pojoLocation=com.lyj.pojo
jdbc.mapperLocation=com.lyj.mapper

4.运行mybatis-generator-maven-plugin

IDEA+maven+mybatis+generator笔记_第1张图片

5.mybatisConfig.xml配置mybatis文件



<configuration>
    <properties resource="db.properties"/>
    
    <typeAliases> 
        <typeAlias alias="User" type="com.lyj.pojo.User"/>
    typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClass}"/>
                <property name="url" value="${jdbc.jdbcUrl}" />
                <property name="username" value="${jdbc.user}"/>
                <property name="password" value="${jdbc.password}"/>
            dataSource>
        environment>
    environments>

    
    <mappers>
        <mapper resource="com/lyj/mapper/UserMapper.xml"/>
    mappers>
configuration>

6.测试

package com.lyj.handler;

import com.lyj.mapper.UserMapper;
import com.lyj.pojo.User;
import com.lyj.pojo.UserExample;
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.Reader;

/**
 * Created by LYJ on 2016/9/29.
 */
public class Test {
    public static void main(String... args) throws Exception {
        Reader reader = Resources.getResourceAsReader("mybatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectByPrimaryKey(1);
        if (user != null)
            System.out.println(user + "      --------- user    --- Test.main\n");

        System.out.println(  "      ---------     --- Test.main\n");

        UserExample example=new UserExample();
        UserExample.Criteria c= example.createCriteria().andIdEqualTo(1);
        User uu=new User();
        uu.setId(1);
        uu.setUserName("QQ");
        //example相当于where后面的限制语句
        //下面这句相当于sql:update user set id=1,name='QQ',password='',thirdId='' where id=1
        userMapper.updateByExample(uu,example);

        sqlSession.commit();
        sqlSession.close();


    }
}

你可能感兴趣的:(java)