• Mybatis Part1


    项目结构如下:

    第一步:添加依赖:

    <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">
        <modelVersion>4.0.0</modelVersion>
        <groupId>tk.mybatis</groupId>
        <artifactId>simple</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <properties>
            <java.version>1.6</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.3.0</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.12</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.12</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    第二步:

     

    <?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="logImpl" value="LOG4J"/>
        </settings>
        
         <typeAliases>
            <package name="tk.mybatis.simple.model"/>
        </typeAliases>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC">
                    <property name="" value=""/>
                </transactionManager>
                <dataSource type="UNPOOLED">
                    <property name="driver" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatisdemo"/>
                    <property name="username" value="root"/>
                    <property name="password" value=""/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper resource="tk/mybatis/simple/mapper/CountryMapper.xml"/>
        </mappers>
    </configuration>

    package tk.mybatis.simple.model;
    
    public class Country {
        private Long id;
        private String countryname;
        private String countrycode;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getCountryname() {
            return countryname;
        }
    
        public void setCountryname(String countryname) {
            this.countryname = countryname;
        }
    
        public String getCountrycode() {
            return countrycode;
        }
    
        public void setCountrycode(String countrycode) {
            this.countrycode = countrycode;
        }
    
    }
    <?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" >
    <mapper namespace="tk.mybatis.simple.mapper.CountryMapper">
        <select id="selectAll" resultType="Country">
            select id,countryname,countrycode from country
        </select>
    </mapper>

    #u5168u5C40u914Du7F6E
    log4j.rootLogger=ERROR, stdout
    
    #MyBatis u65E5u5FD7u914Du7F6E
    log4j.logger.tk.mybatis.simple.mapper=TRACE
    
    #u63A7u5236u53F0u8F93u51FAu914Du7F6E
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

    进行测试:

    package tk.mybatis.simple.mapper;
    
    import java.io.IOException;
    import java.io.Reader;
    import java.util.List;
    
    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 org.junit.BeforeClass;
    import org.junit.Test;
    
    import tk.mybatis.simple.model.Country;
    
    public class CountryMapperTest {
        
        private static SqlSessionFactory sqlSessionFactory;
        
        @BeforeClass
        public static void init(){
            try {
                Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
                reader.close();
            } catch (IOException ignore) {
                ignore.printStackTrace();
            }
        }
        
        @Test
        public void testSelectAll(){
            SqlSession sqlSession = sqlSessionFactory.openSession();
            try {
                List<Country> countryList = sqlSession.selectList("selectAll");
                printCountryList(countryList);
            } finally {
                sqlSession.close();
            }
        }
        
        private void printCountryList(List<Country> countryList){
            for(Country country : countryList){
                System.out.printf("%-4d%4s%4s
    ",country.getId(), country.getCountryname(), country.getCountrycode());
            }
        }
    }

     

  • 相关阅读:
    使用pynlpir增强jieba分词的准确度
    graph easy绘制ascii简易流程图
    kubernetes版本融合解决方案
    设计的一些kubernetes面试题目
    使用go-template自定义kubectl get输出
    docker、oci、runc以及kubernetes梳理
    启动docker容器时的Error response from daemon: devmapper: Error mounting: invalid argument. 错误解决
    kubernetes endpoint一会消失一会出现的问题剖析
    docker启动容器报错: could not synchronise with container process: not a directory
    docker中执行sed: can't move '/etc/resolv.conf73UqmG' to '/etc/resolv.conf': Device or resource busy错误的处理原因及方式
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12332245.html
Copyright © 2020-2023  润新知