1. 程式人生 > >1.mybatis入門

1.mybatis入門

oct ann arc gin system sts getcount sta ins

一:創建表

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入門