首页 > 代码库 > Mybatis增删改查
Mybatis增删改查
Mybatis增删改查
在写这个查所有的用户的时候,JavaBean必须得写一个无参数的构造方法,不然查询所有的用户的时候会出现错误。
package demo2;public class User { private int id; private String name; private int age; public User(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public User() { } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
<?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="demo2.userMapper"> <!-- 根据id查询得到一个user对象 --> <select id="getUser" parameterType="int" resultType="demo2.User"> select * from users where id=#{id} </select> <!-- 添加一个用户 --> <insert id="insertUser" parameterType="demo2.User"> insert into users(name,age)values(#{name},#{age}); </insert> <!-- 修改一个用户的信息 --> <update id="updateUser" parameterType="demo2.User"> update users set name=#{name},age=#{age} where id = #{id} </update> <delete id="deleteUser" parameterType="int"> delete from users where id = #{id} </delete> <select id="selectAllUsers" resultType="demo2.User"> select * from users </select> </mapper>
<?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="db.properties"/> <!-- development : 开发模式 work : 工作模式 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${name}" /> <property name="password" value="${password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="demo2/userMapper.xml"/> </mappers></configuration>
package Test;import java.io.InputStream;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.util.List;import demo2.User;public class Test { public static void main(String[] args) { String resouse = "conf.xml"; //读取上上述的config.xml文件 InputStream is = Test.class.getClassLoader().getResourceAsStream(resouse); SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is); SqlSession session = factory.openSession(true);//在这里如果配置成true就自动提交 String statement = "demo2.userMapper.getUser"; //User user = (User)session.selectOne(statement, 1); String statement2 = "demo2.userMapper.updateUser"; int update = session.update(statement2, new User(1,"张三",14)); //删除User String statement3 = "demo2.userMapper.deleteUser"; int delete = session.delete(statement3, 1); //得到所有的用户 String statement4 = "demo2.userMapper.selectAllUsers"; List<User> list= session.selectList(statement4); System.out.println(list); }}
Mybatis增删改查
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。