码迷,mamicode.com
首页 > 其他好文 > 详细

1.mybatis入门

时间:2018-07-31 15:17:03      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:cto   result   rac   final   reader   mybatis   3.0   pac   代码   

一:创建表

CREATE TABLE `country` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `countryname` varchar(255) DEFAULT NULL,
  `countrycode` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

INSERT INTO `country` VALUES (1, 中国, CN);
INSERT INTO `country` VALUES (2, 美国, US);
INSERT INTO `country` VALUES (3, 俄罗斯, RU);
INSERT INTO `country` VALUES (4, 英国, GB);
INSERT INTO `country` VALUES (5, 法国, FR);

二:项目结构

技术分享图片

 

三.代码:

Country.java

package tk.mybatis.simple.model;

/**
 * 实体类Country
 */
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;
    }

    @Override
    public String toString() {
        return "Country{" +
                "id=" + id +
                ", countryname=‘" + countryname + \‘ +
                ", countrycode=‘" + countrycode + \‘ +
                };
    }
}

CountryMapperTest.java

 

package tk.mybatis.simple.mapper;

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;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.Reader;
import java.util.List;

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 e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testSelectAll(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            List<Country> selectAll = sqlSession.selectList("selectAll");
            System.out.println("selectAll"+selectAll);
        } finally {
            sqlSession.close();
        }

    }
}

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>
    <!--1.用于指定输出LOG4J输出日志-->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <!--2.别名,包名-->
    <typeAliases>
        <package name="tk.mybatis.simple.model"/>
    </typeAliases>

    <!--3.mysql数据库-->
    <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://localhost:3306/bybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="weihu123"/>
            </dataSource>
        </environment>
    </environments>

    <!--4.引入映射文件-->
    <mappers>
        <mapper resource="tk/mybatis/simple/mapper/CountryMapper.xml"/>
    </mappers>
</configuration>

CountryMapper.xml

<?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">
<!--sql语句映射文件-->

<!--因为已经配置了别名,所以resultType可以直接写类名,没有配置的话就要写类全名-->
<mapper namespace="tk.mybatis.simple.mapper.CountryMapper">
   <select id="selectAll" resultType="Country">
       SELECT id,countryname,countrycode FROM country;
   </select>

</mapper>

log4j.properties

#\u5168\u5C40\u914D\u7F6E
log4j.rootLogger=ERROR, stdout

#MyBatis \u65E5\u5FD7\u914D\u7F6E
log4j.logger.tk.mybatis.simple.mapper=TRACE

#\u63A7\u5236\u53F0\u8F93\u51FA\u914D\u7F6E
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

1.mybatis入门

标签:cto   result   rac   final   reader   mybatis   3.0   pac   代码   

原文地址:https://www.cnblogs.com/weihu/p/9395356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!