首先测试文件 的关键是读取配置信息文件,打开数据库会话,操作数据库
测试模块
test.java
package mppperTest;
import model.country;
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.IOException;
import java.io.Reader;
import java.util.List;
public class countryTest {
public static void main(String [] arge){
try {
//读取核心配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
System.out.println("123");
//根据读取的配置文件流创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//打开会话,创建一个会话对象
SqlSession session = sqlSessionFactory.openSession();
reader.close ();
//操作数据库
country countryList = session.selectOne("selectCountId",3);
} catch (IOException e) {
e.printStackTrace();
}
}
}
配置文件
mybatis-config.xml
有默认代码,重要的是数据源的设置,数据库密码等,还有映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mbatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置全局属性-->
<!-- <properties resource="db.properties"/>-->
<!-- 设置 控制台打印sql语句-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!--环境配置-->
<environments default="mysql">
<environment id="mysql">
<!--事物管理器-->
<transactionManager type="JDBC"></transactionManager>
<!--数据源-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybitas?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 映射器 配置映射文件路径//mapper 文件-->
<mappers>
<mapper resource="mapper/countryMapper.xml"/>
<!-- mapper/countryMapper.xml.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">
<mapper namespace="mapper.countryMapper">
<!-- resultMap 配置应用程序中的类和数据库表的关系的 -->
<resultMap id="countryRes" type="model.country">
<!--配置主键-->
<!--
property 对应的是类中属性
column 对应表中的字段
-->
<id property="id" column="id"/>
<result column="country_Name" property="countryName"/>
<!-- <result column="a_password" property="password"/>-->
<!-- <result column="a_addr" property="addr"/>-->
<!-- <result column="a_phone" property="myPhone"/>-->
</resultMap>
<!-- id 很关键是和test使用相关 参数-->
<select id="selectCountId" parameterType="Integer" resultMap="countryRes">
select * from country where id = #{id}
</select>
<!-- <select id="selectAccount" parameterType="Integer" resultMap="accountResult">-->
<!-- select * from account where a_id=#{id}-->
<!-- </select>-->
<!-- <select id="selectAccountForUsernameandpassword" parameterType="cn.rj3.po.Account" resultMap="accountResult">-->
<!-- <!–#{userName} 去调用 传入参数对象的 getUserName()方法–>-->
<!-- select * from account where a_username=#{userName} and a_password=#{password}-->
<!-- </select>-->
</mapper>
model 应用程序的实体模板类
定义数据的内容
package model;
public class country {
int id;
String countryName;
public country() {
}
public country(int id, String countryName) {
this.id= id;
this.countryName= countryName;
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
}