- <?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>
- <typeAliases>
- <typeAlias alias="Goods" type="com.csdn.kane.domain.Goods"/>
- </typeAliases>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/XiaoqingTest" />
- <property name="username" value="root" />
- <property name="password" value="08073440" />
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper class="com.csdn.kane.dao.GoodsMapper"/>
- </mappers>
- </configuration>
4. 建立GoodsMapper接口
- package com.csdn.kane.dao;
- import org.apache.ibatis.annotations.Select;
- import com.csdn.kane.domain.Goods;
- public interface GoodsMapper {
- @Select("SELECT * FROM Goods WHERE id=#{id}")
- public Goods selectGoods(int id);
- }
5. 接下来就可以自己写一个主方法来测试下了
- package com.csdn.kane.test;
- import java.io.IOException;
- import java.io.InputStream;
- 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 com.csdn.kane.dao.GoodsMapper;
- import com.csdn.kane.domain.Goods;
- public class TestMybitas {
- public static void main(String[] args) throws IOException {
- //最基本的mybitas示例方法
- TestMybitas.testMethod();
- }
- public static void testMethod() throws IOException{
- String resource = "mybitasConfigration.xml";
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- try {
- GoodsMapper mapper = session.getMapper(GoodsMapper.class);
- Goods goods = mapper.selectGoods(1);
- System.out.println("good description:"+goods.getDescription());
- } finally {
- session.close();
- }
- }
- }
运