首页 > 代码库 > Mybatis简单的入门之增删改查

Mybatis简单的入门之增删改查

一般的步骤如下
1、添加Mybatis所需要的包,和连接数据库所需的包
2、配置mybatis-config.xml文件

3、配置与pojo对应的映射文件

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><!--类型别名-->
<typeAlias alias="User" type="com.iss.pojo.User" />
</typeAliases>


<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value=http://www.mamicode.com/"com.mysql.jdbc.Driver" />
<property name="url"
value=http://www.mamicode.com/"jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value=http://www.mamicode.com/"root" />
<property name="password" value=http://www.mamicode.com/"root" />
</dataSource>
</environment>
</environments>


<mappers>
<mapper resource="com/iss/pojo/User.xml" /><!--映射文件的路径-->
</mappers>
</configuration>


pojo实体类

package com.iss.pojo;


public class User {
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;
}


public User(int id, String name) {
super();
this.id = id;
this.name = name;
}


public User() {
super();
}


}



User.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="User"><!--映射的命名空间也就是在mybatis中配置的别名-->
<select id="selectUser" parameterType="int" resultType="User"><!--在mybatis-config.xml中为类型起了别名-->
<![CDATA[
        select * from user where id = #{id}]]><!--查询的是表名不是对象-->
</select>
<insert id="insertUser" parameterType="User">

<![CDATA[insert into user
value(#{id},#{name})]]>


</insert>
<delete id="deleteUser" parameterType="int">
delete from user where
id=#{id}


</delete>
<update id="updateUser" parameterType="User">
update user set
name=#{name} where id=#{id}
</update>
</mapper>

Mybatis工具类

Mybatis工具类

package com.iss.util;


import java.io.IOException;
import java.io.Reader;


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.sun.org.apache.xpath.internal.operations.Gte;


public class MybatisUtil {


private static SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();//类似于Hibernate中Configration


private static SqlSessionFactory sqlSessionFactory;//类似于Hibernate中SessionFactory


// 静态初始化块配置文件在类运行时只加载一次
static {


String resource = "mybatis-config.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


sqlSessionFactory = builder.build(reader);
}


private MybatisUtil() {
// TODO Auto-generated constructor stub
}


public static SqlSessionFactoryBuilder getBuilder() {
return builder;
}


public static void setBuilder(SqlSessionFactoryBuilder builder) {
MybatisUtil.builder = builder;
}


      
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}


public static void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
MybatisUtil.sqlSessionFactory = sqlSessionFactory;
}


// 获得sqlSession对象类似于Hibernate中的session
public static SqlSession getSqlSession() {
SqlSession sqlSession = sqlSessionFactory != null ? sqlSessionFactory
.openSession() : null;
return sqlSession;
}


public static void closeSession() {
SqlSession sqlSession = getSqlSession();
if (sqlSession != null) {
sqlSession.close();
}
}


}

UserService

package com.iss.service;


import org.apache.ibatis.session.SqlSession;


import com.iss.pojo.User;
import com.iss.util.MybatisUtil;


public class UserService {


public static User selectUser(int id) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
User user = sqlSession.selectOne("selectUser", 1);
MybatisUtil.closeSession();
return user;
}


public static void insertUser(int id, String name) {


User user = new User();
user.setId(id);
user.setName(name);


SqlSession sqlSession = MybatisUtil.getSqlSession();
sqlSession.insert("insertUser", user);
sqlSession.commit();
MybatisUtil.closeSession();
}


public static void updateUser(int id, String name) {
User user = new User();
user.setId(id);
user.setName(name);


SqlSession sqlSession = MybatisUtil.getSqlSession();
sqlSession.update("updateUser", user);
sqlSession.commit();
MybatisUtil.closeSession();


}


public static void deleteUser(int id) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
sqlSession.delete("deleteUser", id);
sqlSession.commit();
MybatisUtil.closeSession();
}


}

TestUser

package com.iss.test;


import com.iss.pojo.User;
import com.iss.service.UserService;


public class TestUser {


public static void main(String[] args) {


UserService.insertUser(3, "world");
User user = UserService.selectUser(2);
System.out.println(user.getName());
UserService.updateUser(1, "wdy");
System.out.println(UserService.selectUser(2).getName());


UserService.deleteUser(2);
}
}


总结Hibernate 和Mybatis很相似

主要的区别

Hibernate的映射文件是对表的映射

Mybatis的映射文件是对表操作的一种映射