Mybats 实现curd(增删改查)操作
使用Mybats 实现curd操作!
1:加入jar包
2:配置文件的编写(包括两部分,一部分是总的配置文件a[]),另一部分是实体的配置文件!b[]))
a1):编写一个propertity配置文件,内容如下:
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://localhost:3306/mybatis
- username=root
- password=admin
a2):总的配置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>
- <properties resource="com/tuojin/mybatis/res/config.properties" />
- <environments default="tayfer">
- <environment id="tayfer">
- <transactionManager type="JDBC" />
- <dataSource type="POOLED">
- <property name="driver" value="${driver}" />
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="com/tuojin/mybatis/entrys/Student.xml" />
- </mappers>
- </configuration>
b1):映射类接口 com.tuojin.mybatis.mapper.StuMapper
public interface StuMapper{
public void addStu(Student stu);
public int deleteStu(int id);
public Student selectStu(int id);
public Student findAllStu();
public int updateStu(Student stu);
}
b2):映射类(实体)配置Map.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.tuojin.mybatis.mapper.StuMapper">
- <resultMap type="com.tuojin.mybatis.entrys.Student" id="stuRes">
- <id property="id" column="id" javaType="int" jdbcType="INTEGER" />
- <result property="name" column="name" javaType="String"
- jdbcType="VARCHAR" />
- <result property="version" column="version" javaType="String"
- jdbcType="VARCHAR" />
- <result property="maker" column="maker" javaType="String"
- jdbcType="VARCHAR" />
- </resultMap>
- <insert id="addStu" useGeneratedKeys="true" keyProperty="id">
- insert
- into tb_student(name,version,maker)
- values(#{name},#{version},#{maker})
- </insert>
- <delete id="deleteStu" parameterType="int">
- delete from tb_student
- where
- id=#{id}
- </delete>
- <select id="selectStu" parameterType="int" resultMap="stuRes">
- select *
- from tb_student where id=#{id}
- </select>
- <select id="findAllStu" resultMap="stuRes">
- select * from tb_student
- </select>
- <update id="updateStu" parameterType="com.tuojin.mybatis.entrys.Student">
- update tb_student set
- name=#{name},maker=#{maker},version=#{version} where id=#{id}
- </update>
- </mapper>
2:使用一个工具类,获取类似hibernate session
- public class MybatisUtils
- {
- private static SqlSessionFactory sqlSessionFactory = null;
- private static SqlSession session = null;
- public static SqlSessionFactory getSqlSessionFactory()
- {
- return sqlSessionFactory;
- }
- public static SqlSession getSession()
- {
- return sqlSessionFactory.openSession();
- }
- static
- {
- String URL = "com/tuojin/mybatis/res/sqlMapClient.xml";
- try
- {
- Reader reader = org.apache.ibatis.io.Resources
- .getResourceAsReader(URL);
- sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
- }
- catch (IOException e)
- {
- System.err.println(e.getMessage());
- }
- }
- public static void closeSession(SqlSession session)
- {
- session.close();
- }
4:对数据库进行操作!
- public class TestMyBatis
- {
- @Test
- public void testConfig()
- {
- MybatisUtils.getSession();
- }
- @Test
- public void testAdd()
- {
- Student stu = new Student();
- stu.setMaker("80");
- stu.setName("tayfer");
- stu.setVersion("100_1");
- SqlSession session = MybatisUtils.getSession();
- session.insert("addStu", stu); //IUserInfo mapper = session.getMapper(IUserInfo.class); mapper.insertUser(ui);
- session.commit();
- MybatisUtils.closeSession(session);
- }
- @Test
- public void testDel()
- {
- SqlSession session = MybatisUtils.getSession();
- int flag = session.delete("deleteStu", 17);
- session.commit();
- MybatisUtils.closeSession(session);
- System.err.println(flag);
- }
- @Test
- public void testSelect()
- {
- SqlSession session = MybatisUtils.getSession();
- Student stu = (Student) session.selectOne("selectStu", 18);
- session.commit();
- MybatisUtils.closeSession(session);
- System.err.println(stu);
- }
- @Test
- public void testUpdate()
- {
- SqlSession session = MybatisUtils.getSession();
- Student stu = (Student) session.selectOne("selectStu", 18);
- stu.setName("yuyu");
- int flag = session.update("updateStu", stu);
- session.commit();
- MybatisUtils.closeSession(session);
- System.err.println(flag);
- }