今天开始学习mybatis,主要对一个简单的表实现增删改查
1.先在数据库中创建一个category表,包含两个字段,id和name
2.定义实体类Category
3.定义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="pojo"></package>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="test"></property>
<property name="password" value="1"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="pojo/Category.xml"></mapper>
</mappers>
</configuration>
4.在pojo中创建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="pojo">
<insert id="addCategory" parameterType="Category">
insert into category values(#{id},#{name})
</insert>
<delete id="deleteCategory" parameterType="Category">
delete from category where id=#{id}
</delete>
<select id="listCategory" resultType="Category">
select * from category
</select>
<update id="updateCategory" parameterType="Category">
update category SET name=#{name} WHERE id =#{id}
</update>
<select id="getCategory" resultType="Category">
select * from category where id=#{id}
</select>
</mapper>
5.可以先创建一个SessionFactory单例模式类
public class SessionFactory{
private static SqlSessionFactory factory = null;
private SessionFactory (){}
public static SqlSessionFactory getSessionFactory()throws IOException{
if(factory != null){
return factory;
}
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
factory = sqlSessionFactory;
return factory;
}
}
6.再创建实现增删改查的操作接口和类
package service;
import factory.SessionFactory;
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 pojo.Category;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Created by Administrator on 2018/4/26/026.
*/
public class CategoryServiceImpl implements CategoryService{
CategoryService service;
SqlSession session = null;
@Override
public void add(Category category) {
try {
session = SessionFactory.getSessionFactory().openSession();
session.insert("addCategory",category);
session.commit();
System.out.println("添加成功");
}catch(Exception e){
e.printStackTrace();
}finally {
if (session != null) {
session.close();
}
}
}
@Override
public void delete(int id) {
try {
session = SessionFactory.getSessionFactory().openSession();
session.delete("deleteCategory",id);
session.commit();
System.out.println("删除成功");
}catch(Exception e){
e.printStackTrace();
}finally {
if (session != null) {
session.close();
}
}
}
@Override
public void update(int id,String name) {
try {
session = SessionFactory.getSessionFactory().openSession();
Category c = session.selectOne("getCategory",id);
c.setName(name);
session.update("updateCategory",c);
session.commit();
System.out.println("修改成功");
}catch(Exception e){
e.printStackTrace();
}finally {
if (session != null) {
session.close();
}
}
}
public List<Category> list(){
List<Category> list = null;
try {
session = SessionFactory.getSessionFactory().openSession();
list = session.selectList("listCategory");
for(Category c : list){
System.out.println(c.toString());
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (session != null) {
session.close();
}
}
return list;
}
}
7.创建一个测试类
public class TestMybatis {
public static void main(String[] args) throws IOException{
CategoryServiceImpl service = new CategoryServiceImpl();
Category c = new Category();
c.setId(3);
c.setName("新增加的category");
service.add(c);
service.list();
service.delete(3);
service.list();
service.update(3,"修改过的category");
service.list();
}
}