@[toc]
Mybatis实现增删改查
一.mybatis的配置
1.1 添加相应的jar包
在lib文件夹下面添加mybatis的核心jar包以及依赖的jar包
同在lib文件夹下面加入mysql的驱动jar包
1.2 配置mybatis.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文件-->
<properties resource="db.properties"></properties>
<settings>
<!-- 配置日志的输出形式 -->
<setting name="logImpl" value="LOG4J" />
</settings>
<!-- 配置数据库的连接 默认使用哪一个数据库连接 -->
<environments default="mysql">
<!-- 配置mysql -->
<environment id="mysql">
<!-- 事务管理 使用JDBC -->
<transactionManager type="JDBC"></transactionManager>
<!-- type 连接属性
UNPOOLED-这个类型的数据源实现只是在每次需要的时候简单地打开和关闭连接。
POOLED-这个数据源的实现缓存了JDBC 连接对象,用于避免每次创建新的数据库连接时都初始 化和进行认证,加快程序响应。并发WEB应用通常通过这种做法来获得快速响应。
NDI- 这个数据源的配置是为了准备与像gpring或应用服务器能够在外部或者内部配置数据 源的容器一起使用,然后在NDI 上下文中引用它 -->
<dataSource type="POOLED">
<property name="driver" value="${diver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- 配置映射 -->
<mappers>
<mapper resource="com/sxt/mapping/UserMapper.xml" />
</mappers>
</configuration>
1.3 创建数据库
注意:在设计数据库时,id的类型为int并且设置为主键。自动递增。
1.4 创建实体类
package com.sxt.domain;
public class User {
private Integer id;
private String name;
private String address;
private String sex;
public User(){
}
//插入
public User(String name, String address, String sex) {
super();
this.name = name;
this.address = address;
this.sex = sex;
}
//查询
public User(Integer id, String name, String address, String sex) {
super();
this.id = id;
this.name = name;
this.address = address;
this.sex = sex;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", address=" + address + ", sex=" + sex + "]";
}
}
1.5 创建接口实现的方法
package com.sxt.mapper;
import com.sxt.domain.User;
import java.util.List;
public interface UserMapper {
public void add(User user);
public void update(User user);
public void delete(Integer id); //删除的构造方法
public User queryById(Integer id);//查询的构造方法
public List<User> queryAll();
public List<User> queryLike(User user); //模糊查询
}
1.6 配置UserMapper.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引入头文件 -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sxt.mapper.UserMapper"> <!-- namespace一般与Mapper接口的限定名一致 -->
<!--添加
id对应接口里面的方法名
parameterType参数类型
-->
<insert id="add" parameterType="com.sxt.domain.User">
insert into user (name,address,sex) values(#{
name},#{
address},#{
sex})
</insert>
<!-- 修改 -->
<update id="update" parameterType="com.sxt.domain.User">
update user set name=#{
name},address=#{
address},sex=#{
sex} where id=#{
id}
</update>
<!-- 删除 -->
<delete id="delete" parameterType="java.lang.Integer">
delete form user where id=#{
value}
</delete>
<!--查询一个
resultType返回值的类型
-->
<select id="queryById" parameterType="java.lang.Integer" resultType="com.sxt.domain.User">
select * from user where id=#{
value}
</select>
<!-- 全查询 -->
<select id="queryLike" resultType="com.sxt.domain.User">
<!-- select * from user where name like #{
name} -->
select * from user where name like "%"#{
name}"%"
</select>
<!-- 模糊查询 -->
</mapper>
1.7 创建MybatisUtils的方法
package com.sxt.utils;
import java.io.InputStream;
import javax.websocket.Session;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtils {
static InputStream is=MybatisUtils.class.getResourceAsStream("/mybatis.xml");
//得到SqlSessionFactory
static SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
//得到session
public static SqlSession openSession() {
return factory.openSession();
}
//关闭session
public static void closeSession(SqlSession session) {
//提交
session.commit();
//关闭
session.close();
}
}
二、Mybatis的增删改查
2.1 添加
package com.sxt.text;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.sxt.domain.User;
import com.sxt.mapper.UserMapper;
import com.sxt.utils.MybatisUtils;
public class mybatisTest {
public static void main(String[] args) {
SqlSession session=MybatisUtils.openSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
// TODO Auto-generated method stub
User user=new User("小花", "武汉", "男");
userMapper.add(user);
System.out.println("chen");
MybatisUtils.closeSession(session);
}
2.2 修改
package com.sxt.text;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.sxt.domain.User;
import com.sxt.mapper.UserMapper;
import com.sxt.utils.MybatisUtils;
public class mybatisTest {
// SqlSession session=MybatisUtils.openSession();
// UserMapper userMapper=session.getMapper(UserMapper.class);
// @Test
// public void initData(){
public static void main(String[] args) {
SqlSession session=MybatisUtils.openSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
// TODO Auto-generated method stub
// User user=new User("小明", "武汉", "男");
User user=new User(2,"小花","河北","女");
userMapper.update(user);
MybatisUtils.closeSession(session);
}
2.3 查询
package com.sxt.text;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.sxt.domain.User;
import com.sxt.mapper.UserMapper;
import com.sxt.utils.MybatisUtils;
public class mybatisTest {
// SqlSession session=MybatisUtils.openSession();
// UserMapper userMapper=session.getMapper(UserMapper.class);
// @Test
// public void initData(){
public static void main(String[] args) {
SqlSession session=MybatisUtils.openSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
// TODO Auto-generated method stub
// User user=new User("小明", "武汉", "男");
//User user=new User(2,"小花","河北","女");
List<User> list=userMapper.queryAll();
System.out.println(list);
MybatisUtils.closeSession(session);
}
2.4 删除
package com.sxt.text;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.sxt.domain.User;
import com.sxt.mapper.UserMapper;
import com.sxt.utils.MybatisUtils;
public class mybatisTest {
// SqlSession session=MybatisUtils.openSession();
// UserMapper userMapper=session.getMapper(UserMapper.class);
// @Test
// public void initData(){
public static void main(String[] args) {
SqlSession session=MybatisUtils.openSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
// TODO Auto-generated method stub
// User user=new User("小明", "武汉", "男");
//User user=new User(2,"小花","河北","女");
// List<User> list=userMapper.queryAll();
//System.out.println(list);
userMapper.delete(3);
MybatisUtils.closeSession(session);
}
三、总结
好了,今天的分享就到这里了