MyBatis
1.首先要准备:
2.实体类与小配置
//实体类 public class Dept { private Integer deptNo; private String deptName; public Integer getDeptNo() { return deptNo; } public void setDeptNo(Integer deptNo) { this.deptNo = deptNo; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } }
//小配置 XXX.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="my.entity.Dept">
<!-- 查询部门所有信息 -->
<select id="selectAllDept" resultType="Dept">
select * from MyDept
</select>
<!--条件查询 -->
<select id="selectAllDeptById" parameterType="Dept" resultType="Dept">
select * from MyDept where deptNo=#{deptNo}
</select>
<!-- 删除 -->
<delete id="deleteAllDeptById" parameterType="Dept" >
delete from dept where deptNo=#{deptNo}
</delete>
<!-- 修改 -->
<update id="updateAllDeptById" parameterType="Dept" >
update dept set deptName=#{deptName} where deptNo=#{deptNo}
</update>
<!-- 增加 -->
<insert id="insertAllDeptById" parameterType="Dept" >
insert into dept values(#{deptNo},#{deptName},#{deptCity})
</insert>
</mapper>
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> <!-- 别名的配置 Dept--> <typeAliases> <typeAlias type="my.entity.Dept" alias="Dept"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:LIYANG" /> <property name="username" value="LiYang" /> <property name="password" value="orcl" /> </dataSource> </environment> </environments> <mappers> <mapper resource="my/entity/Dept.xml" /> </mappers> </configuration>
4. 测试类
SqlSession session;
@Before
public void initData() throws IOException{
Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
session= factory.openSession();
}
@Test
public void testselectAllDept() throws IOException{
//在xml配置中的一个锁定唯一SQL的id
List<Dept> selectList = session.selectList("selectAllDept");
for (Dept dept : selectList) {
System.out.println(dept);
}
}
//模糊查詢
@Test
public void likeTest(){
Dept dept = new Dept();
dept.setDeptName("市场");
List<Dept> list = session.selectList("mybatis.entity.Dept.likeDept",dept);
for (Dept item : list) {
System.out.println(item);
}
session.close();
}
//修改
@Test
public void updateTest(){
Dept dept = new Dept();
dept.setDeptNo(5);
dept.setDeptName("开发部");
int count = session.update("mybatis.entity.Dept.updateDept",dept);
session.commit();
System.out.println(count+"修改成功");
session.close();
}
//删除
@Test
public void testdeleteDept() throws IOException{
Dept dept = new Dept();
dept.setDeptNo(5);
int count = session.delete("mybatis.entity.Dept.deleteDept",dept);
session.commit();
System.out.println(count+"删除成功!");
}
//增加
@Test
public void testinsertDept() throws IOException{
Dept dept = new Dept();
dept.setDeptNo(8);
dept.setDeptName("财务部");
dept.setDeptCity("上海");
int count = session.insert("mybatis.entity.Dept.insertDept",dept);
session.commit();
System.out.println(count+"增加成功");
}
}