第一步打开intellij idea建立一个java 项目,我这里起名叫mybatis
第二步在项目跟目录建立lib目录,把需要的两个jar包导入
mybatis-3.4.2.jar
mysql-connector-java-5.0.8-bin.jar
第三步在src下建立这样一个包com.how2java.pojo,然后在建立个文件mybatis-config.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="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/how2java/pojo/Category.xml"/>
</mappers>
</configuration>
接着在com.how2java.pojo包下面建立一个类
Category:
package com.how2java.pojo;
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = 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>
<delete id="deleteCategory" parameterType="Category">
delete from category_ where id=#{id}
</delete>
<update id="updateCategory" parameterType="Category">
update category_ set name=#{name} where id=#{id}
</update>
<select id="getCategory" parameterType="Category" resultType="Category">
select * from category_ where id=#{id}
</select>
<select id="listCategory" resultType="Category">
select * from category_
</select>
</mapper>
然后在how2java包下创建测试类:
TestMybatis代码如下:
package com.how2java;
import java.io.IOException;
import java.io.InputStream;
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 com.how2java.pojo.Category;
public class TestMybatis {
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();
/**
* 插入一条记录
*/
Category c1 = new Category();
c1.setName("新增加的Category");
session.insert("addCategory", c1);
/**
* 删除一条记录
*/
Category c2 = new Category();
c2.setId(3);
session.delete("deleteCategory",c2);
/**
* 获取一条记录
*/
Category c3 = session.selectOne("getCategory", 2);
//System.out.println(c3.getName());
c3.setName("tom");
session.update("updateCategory", c3);
/**
* 查询所有记录
*/
List<Category> cs = session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getName());
}
}
}
ok,这里只是记录一下mybatis的基本用法,以备需要的时候使用!
贴出两个jar包的下载地址:链接:https://pan.baidu.com/s/1k-JgDI1urhPxD0mM5Ldtlw 提取码:oswc
demo下载地址:链接:https://pan.baidu.com/s/1RLVfRjqo6GnVb5jWdl__NA 提取码:jwl0