mybatis IDEA 静态查询基本使用

首先测试文件 的关键是读取配置信息文件,打开数据库会话,操作数据库
测试模块
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">-->
<!--        &lt;!&ndash;#{userName}  去调用 传入参数对象的 getUserName()方法&ndash;&gt;-->
<!--        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;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值