一酪耕、基本概念
- What for
JDBC訪問數(shù)據(jù)庫命迈,除了需要自己寫SQL之外呼寸,還必須操作Connection, Statment, ResultSet 這些其實只是手段的輔助類识樱。 不僅如此嗤无,訪問不同的表,還會寫很多雷同的代碼怜庸,顯得繁瑣和枯燥当犯。
Mybatis之后只需要自己提供SQL語句。其他的工作割疾,諸如建立連接嚎卫,Statement, JDBC相關(guān)異常處理等等都交給Mybatis去做了宏榕,那些重復(fù)性的工作Mybatis也給做掉了拓诸,我們只需要關(guān)注在增刪改查等操作層面上,而把技術(shù)細(xì)節(jié)都封裝在了我們看不見的地方麻昼。
- 原 理
- 應(yīng)用程序找Mybatis要數(shù)據(jù)
- mybatis從數(shù)據(jù)庫中找來數(shù)據(jù)
a)通過mybatis-config.xml 定位哪個數(shù)據(jù)庫 (mybatis-config.xml配置數(shù)據(jù)庫連接信息)
b) 通過Category.xml執(zhí)行對應(yīng)的select語句 (Category.xml將sql語句與實體建立對應(yīng)關(guān)系)
c) 基于Category.xml把返回的數(shù)據(jù)庫記錄封裝在Category對象中
d) 把多個Category對象裝在一個Category集合中 -
返回一個Category集合
二奠支、配置方式Crud
兩個配置文件:
mybatis-config.xml:建立數(shù)據(jù)庫連接
Category.xml:將sql語句與實體建立對應(yīng)關(guān)系
- mybatis-config.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>
<typeAliases>
<package name="com.how2java.pojo"/>
</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://localhost:3306/how2java?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/how2java/pojo/Category.xml"/>
</mappers>
</configuration>
下面分別列出增刪改查中,Category.xml 及 java 代碼片段:
1.增
addCategory對應(yīng)的插入sql語句抚芦,#{name}會自動獲取c對象的name屬性值
- Category.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.how2java.pojo">
<!-- 增 -->
<insert id="addCategory" parameterType = "Category" >
insert into category_ (name) values (#{name})
</insert>
</mapper>
- TestMybatis.java
Category c = new Category();
c.setName("新增加的Category");
session.insert("addCategory",c); // 第一個參數(shù)為sql語句的id,第二個參數(shù)類型與sql中的parameterType一致
2.刪
- Category.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.how2java.pojo">
<!-- 增 -->
<insert id="addCategory" parameterType = "Category" >
insert into category_ (name) values (#{name})
</insert>
</mapper>
- TestMybatis.java
Category c = new Category();
c.setName("新增加的Category");
session.insert("addCategory",c); // 第一個參數(shù)為sql語句的id,第二個參數(shù)類型與sql中的parameterType一致
3.改
- Category.xml
<update id="updateCategory" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
- TestMybatis.java
Category c= session.selectOne("getCategory",3);
c.setName("修改了的Category名稱");
session.update("updateCategory",c);
4.查
查詢所有
- Category.xml
<select id="listCategory" resultType="Category">
select * from category_
</select>
- TestMybatis.java
List<Category> cs = session.selectList("listCategory");
三倍谜、注解方式Crud
一個配置文件:
mybatis-config.xml:建立數(shù)據(jù)庫連接
CategoryMapper 取代了Category.xml
- mybatis-config.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>
<typeAliases>
<package name="com.how2java.pojo"/>
</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://localhost:3306/how2java?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.how2java.mapper.CategoryMapper"/>
</mappers>
</configuration>
- CategoryMapper.java
public interface CategoryMapper {
@Insert(" insert into category_ ( name ) values (#{name}) ")
public int add(Category category);
@Delete(" delete from category_ where id= #{id} ")
public void delete(int id);
@Select("select * from category_ where id= #{id} ")
public Category get(int id);
@Update("update category_ set name=#{name} where id=#{id} ")
public int update(Category category);
@Select(" select * from category_ ")
public List<Category> list();
}
- TestMybatis.java
public static void main(String[] args) throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
CategoryMapper mapper = session.getMapper(CategoryMapper.class);
// add(mapper);
// delete(mapper);
// get(mapper);
// update(mapper);
listAll(mapper);
session.commit();
session.close();
}
private static void update(CategoryMapper mapper) {
Category c= mapper.get(8);
c.setName("修改了的Category名稱");
mapper.update(c);
listAll(mapper);
}
private static void get(CategoryMapper mapper) {
Category c= mapper.get(8);
System.out.println(c.getName());
}
private static void delete(CategoryMapper mapper) {
mapper.delete(2);
listAll(mapper);
}
private static void add(CategoryMapper mapper) {
Category c = new Category();
c.setName("新增加的Category");
mapper.add(c);
listAll(mapper);
}
private static void listAll(CategoryMapper mapper) {
List<Category> cs = mapper.list();
for (Category c : cs) {
System.out.println(c.getName());
}
}