这篇文章主要介绍了mybatis如何实现增删改查,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Mybatis实现增删改查一.mybatis的配置1.1 添加相应的jar包在l
这篇文章主要介绍了mybatis如何实现增删改查,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
Mybatis实现增删改查
在lib文件夹下面添加mybatis的核心jar包以及依赖的jar包
同在lib文件夹下面加入mysql的驱动jar包
<?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>
注意:在设计数据库时,id的类型为int并且设置为主键。自动递增。
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 + "]"; } }
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); //模糊查询}
<?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>
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(); }}
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); }
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); }
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); }
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); }
感谢你能够认真阅读完这篇文章,希望小编分享的“Mybatis如何实现增删改查”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网精选频道,更多相关知识等着你来学习!
--结束END--
本文标题: Mybatis如何实现增删改查
本文链接: https://lsjlt.com/news/307004.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0