洞察纵观鸿蒙next版本,如何凭借FinClip加强小程序的跨平台管理,确保企业在数字化转型中的高效运营和数据安全?
756
2022-11-08
Mybatis实现增删改查
目录一.mybatis的配置1.1 添加相应的jar包1.2 配置mybatis.xml文件1.3 创建数据库1.4 创建实体类1.5 创建接口实现的方法1.6 配置UserMapper.xml 文件1.7 创建MybatisUtils的方法二、Mybatis的增删改查2.1 添加2.2 修改2.3 查询2.4 删除
Mybatis实现增删改查
一.mybatis的配置
1.1 添加相应的jar包
在lib文件夹下面添加mybatis的核心jar包以及依赖的jar包同在lib文件夹下面加入mysql的驱动jar包
1.2 配置mybatis.xml文件
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
public List
}
1.6 配置UserMapper.xml 文件
insert into user (name,address,sex) values(#{name},#{address},#{sex})
update user set name=#{name},address=#{address},sex=#{sex} where id=#{id}
delete form user where id=#{value}
select * from user where id=#{value}
select * from user where name like "%"#{name}"%"
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 sthttp://atic 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
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
//System.out.println(list);
userMapper.delete(3);
MybatisUtils.closeSession(session);
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~