关闭

mybatis入门之CRUD (XML版)

标签: mybatis
92人阅读 评论(0) 收藏 举报

接上一篇博客:

1.建立user表和实体类

2.编写userMapper.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.mybits_01.test2.userMapper">
<!-- 
CRUD
 -->
 <insert id="addUser" parameterType="com.mybits_01.test1.User">
  insert into users(name, age) values(#{name},#{age})
 </insert>
 <delete id="deleteUser" parameterType="int">
  delete from users where id = #{id}
 </delete>
 <update id="updateUser" parameterType="com.mybits_01.test1.User">
  update users set name=#{name},age=#{age} where id=#{id}
 </update>
 
 <select id="getUser" parameterType="int"
 resultType="com.mybits_01.test1.User">
 select * from users where id=#{id}
 </select>
 
 <select id="getAllUsers" resultType="com.mybits_01.test1.User">
 select * from users
 </select>
</mapper>

3.在config.xml中注册userMapper.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>


<environments default="development">
<!-- 
environments有二种模式
development:开发模式 
work:工作模式           

environments的default要与environment的id一致。要么全是development要么全是work

transactionManager 事务管理器,事务提交 
二种事务管理器类型 jdbc/manager
jdbc:自己做事务的提交和回滚
manager就是交给容器(如:spring)去管理

数据源 dataSource
连接池
1.POOLED
2.UNPOOLED
3.JNDI
-->
<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/mybatis" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
<!-- 注册 userMapper.xml-->
<mappers>
<mapper resource="com/mybits_01/test1/userMapper.xml"/>
<mapper resource="com/mybits_01/test2/userMapper.xml"/>
</mappers>
</configuration>

4.为方便编写一个连接的Util类

package com.mybits_01.util;


import java.io.InputStream;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import com.mybits_01.test1.Test;


public class MybatisUtil {
public static SqlSession getSession() {
String resource = "conf.xml";
InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//提交方式手动提交
SqlSession session = factory.openSession();

return session;
}
}

5.测试test

package com.mybits_01.test2;


import java.util.List;


import org.apache.ibatis.session.SqlSession;
import org.junit.Test;


import com.mybits_01.test1.User;
import com.mybits_01.util.MybatisUtil;


public class Test2 {

@Test
public void testAdd() {
SqlSession session = MybatisUtil.getSession();
String statement = "com.mybits_01.test2.userMapper.addUser";
int n = session.insert(statement, new User(-1, "kk", 23));
session.commit();
System.out.println("成功添加"+n+"条数据");
}

@Test
public void testUpdate() {
SqlSession session = MybatisUtil.getSession();
String statement = "com.mybits_01.test2.userMapper.updateUser";
int n = session.update(statement, new User(3, "aa", 25));
session.commit();
System.out.println("成功更新"+n+"条数据");
}

@Test
public void testDelete() {
SqlSession session = MybatisUtil.getSession();
String statement = "com.mybits_01.test2.userMapper.deleteUser";
int n = session.delete(statement, 3);
session.commit();
System.out.println("成功删除"+n+"条数据");
}

@Test
public void testGetUser() {
SqlSession session = MybatisUtil.getSession();
String statement = "com.mybits_01.test2.userMapper.getUser";
User user = session.selectOne(statement, 1);
System.out.println(user.getName());
}

@Test
public void testGetAllUsers() {
SqlSession session = MybatisUtil.getSession();
String statement = "com.mybits_01.test2.userMapper.getAllUsers";
List<User> list = session.selectList(statement);
System.out.println(list);
}
}


1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:33716次
    • 积分:794
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:96篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论