新建一张test表,有id,name两个字段
配置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.yzh.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/yzh_mybatis?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/yzh/pojo/test.xml"/>
</mappers>
</configuration>
新建表对应的实体类test.java 生成get/set方法
package com.yzh.pojo;
public class Test {
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;
}
}
新建实体映射类配置文件test.xml
在配置文件里面写入crud方法,如下
<?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.yzh.pojo">
<insert id="addTest" parameterType="Test" >
insert into test ( name ) values (#{name})
</insert>
<delete id="deleteTest" parameterType="Test" >
delete from test where id= #{id}
</delete>
<select id="getTest" parameterType="_int" resultType="Test">
select * from test where id= #{id}
</select>
<update id="updateTest" parameterType="Test" >
update test set name=#{name} where id=#{id}
</update>
<select id="listTest" resultType="Test">
select * from test
</select>
</mapper>
新建一个测试类
package com.yzh.test;
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.yzh.pojo.Test;
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();
Test test=new Test();
test.setId(3);
test.setName("新增的test3");
session.insert("addTest", test);
listAll(session);
session.commit();
session.close();
}
private static void listAll(SqlSession session) {
List<Test> cs=session.selectList("listTest");
for (Test c : cs) {
System.out.println(c.getName());
}
}
}
运行测试类获取表里面的name集合
数据库表里面的name集合
下面是删除操作,在测试类里面修改
//删除 根据id删除
Test test=new Test();
test.setId(2);
session.insert("deleteTest", test);
//修改 根据id修改
Test test=new Test();
test.setId(3);
test.setName("修改的test3");
session.insert("updateTest", test);
//查看
Test c= session.selectOne("getTest",3);
System.out.println(c.getName());
java学习 http://h5ip.cn/study