【MyBatis】第一个MyBatis工程
本文最后更新于 1531 天前,其中的信息可能已经有所发展或是发生改变。

1.创建java项目并导入jar包。
mybatis-3.2.2.jar:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/jar包/mybatis-3.2.2.jar[/bdbtn]
mysql-connector-java-5.1.18-bin.jar:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/jar包/mysql-connector-java-5.1.18-bin.jar[/bdbtn]
2.配置MyBatis框架。
在src里面配置mybatis.xml。
[sourcecode language=”java” title=”mybatis.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>
<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/spring?useUnicode=true&amp;characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bean/GoodsType.xml" />
</mappers>
</configuration>
[/sourcecode]
3.建立数据库。
这里给个数据库文件:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/数据库/spring.sql[/bdbtn]
4.编写实体类:
[sourcecode language=”java” title=”com.bean.GoodsType.java”]
package com.bean;

public class GoodsType {
private int typeId;
private String typeName;
public GoodsType(int typeId, String typeName) {
super();
this.typeId = typeId;
this.typeName = typeName;
}
public GoodsType() {
}
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
@Override
public String toString() {
return "GoodsType [typeId=" + typeId + ", typeName=" + typeName + "]";
}

}
[/sourcecode]
5.编写一个与类同名的xml文件。
[sourcecode language=”java” title=”com.bean.GoodsType.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="com.bean.GoodsType">
<select id="findAll" resultType="com.bean.GoodsType">
select * from goods_type
</select>
</mapper>
[/sourcecode]
6.编写测试的类。
[sourcecode language=”java” title=”com.test.test.java”]
package com.test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

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.Before;
import org.junit.Test;

import com.bean.GoodsType;

public class test {

private SqlSession sqlSession;

public void test01() {
try {
List<GoodsType> goodsTypeList = this.sqlSession.selectList("com.bean.GoodsType.findAll");
for (GoodsType goodsType : goodsTypeList) {
System.out.println(goodsType);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
this.sqlSession.close();
}
}

@Test
public void testF(){
test01();
}

@Before
public void before() {
try {
String resource = "mybatis.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
this.sqlSession = ssf.openSession();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[/sourcecode]
7.运行测试类即可。

源代码项目下载:[bdbtn]https://pan.benzhu.xyz/代码/源代码/MyBatis/源代码/MB001.rar[/bdbtn]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇