第一次利用官方文檔學習知識,曾經覺得看官網挺難的,今天耐心的看了下去,發現還是挺不錯的,這裏詳細的分享,帶小白Mybatis入門。Mybatis官方教程後面不懂的地方可以查看,官網有中文版。
學什麼都最好有實例,所以我們用一個實例來帶學習。
首先,我們設計下數據庫,如圖:
這裏解釋提示下爲什麼數據類型是這樣:
數據類型 | 佔用存儲空間 | 定義格式 |
---|---|---|
Char(n) | 定長,最大8KB | 存ANSI字符,n爲串長度 |
Varchar(n) | 變長,最大8KB | 存ANSI字符,n爲串長度 |
Nchar(n) | 定長,最大8KB | 存Unicode字符,n爲串長度 |
Nvarchar(n) | 變長,最大8KB | 存Unicode字符,n爲串長度 |
** | ANSI | Unicode |
---|---|---|
英文字符 | 1字節 | 2字節 |
漢字 | 2字節 | 2字節 |
如上表所示,例:
Char(5):即長度爲5,如果長爲5,比如’21545’,即可以存儲,如果你存的是’211’,長度爲3,而Char是定長,所以211後面會補2個空格。讓其強行成爲長爲5的串,所以Char適合定長。並且Char存的ANSI字符,所以如上表,一個英文佔一個字節,而一箇中文佔兩個字節,’西昌6’和’21545’佔相同字節,Char適合全英文。
Varchar(5):即長度爲5,如果存的數據佔5個字節,比如’21545’,即可以存儲,如果你存的是’211’,只有3個字節,而Varchar是變長,依然存儲’211’,不補充空格。
NChar(5):即長度爲5,’西昌666’和’仙劍奇俠傳’就佔相同字節,所以NChar適合中/英混合。
接下來我們創建項目,如圖所示:
創建實體類:
public class Student {
private int ID;
private String Name;
private String Phone;
private String Age;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getAge() {
return Age;
public void setAge(String age) {
Age = age;
}
public Student(String name, String phone, String age) {
super();
Name = name;
Phone = phone;
Age = age;
}
public Student() {}
}
導入Mybatis和jdbc的包,創建Configuration.xml文件,可以在你下載的Mybatis壓縮包中找到這個xml文件,複製過來,這裏用的SQL Server2008,自己可以配置數據庫的名字,密碼,驅動等,如下:
<?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>
<settings>
<setting name="useGeneratedKeys" value="false"/>
<setting name="useColumnLabel" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=MybatisTest"/>
<property name="username" value="sa"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--
mapper是必須的,讀取另一個xml,後面的Student.xml文件
-->
<mappers>
<mapper resource="sqlxml/Student.xml"/>
</mappers>
</configuration>
再創建Student.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="Student">
<!--type哪一個類 -->
<resultMap type="com.entity.Student" id="StudentResult">
<id column="ID" jdbcType="INTEGER" property="ID"/> <!-- id爲主鍵標籤 result爲普通標籤-->
<result column="Name" jdbcType="NVARCHAR" property="Name"/>
<result column="Phone" jdbcType="NCHAR" property="Phone"/>
<result column="Age" jdbcType="NCHAR" property="Age"/>
</resultMap>
<!-- 找到對應關係resultmap會進行賦值 -->
<select id="queryStudentList" parameterType="com.entity.Student" resultMap="StudentResult">
select * from Student where Name=#{name}
</select>
</mapper>
以上配置完成之後就可以開始測試了。
public class DBTest {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
SqlSession session = null;
try {
//此處爲官網推薦做法
InputStream inputStream = Resources.getResourceAsStream("configure/Configuration.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
//這裏是參數查詢的設置,查詢名字爲呂布的學生
Student student = new Student();
student.setName("呂布");
list = session.selectList("Student.queryStudentList", student);
list.forEach((stu) -> System.out.println(stu.getName()+" "+stu.getAge()+" "+stu.getPhone()));
} catch (IOException e) {
e.printStackTrace();
} finally {
//每次用完都要關閉
if(session!=null){
session=null;
}
}
}
}
以上就完成了查詢操作了,是不是很簡單,update以及刪除和這類似,可以去官網查看,就不多說啦。