mybatis入门之简单操作

mybatis入门之简单操作

环境配置

1.准备jar包


2.编写配置文件

SqlMapConfig.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>
   <environments default="developement">
      <environment id="developement">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
          <property name="driver" value="com.mysql.jdbc.Driver"/>
          <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"/>
          <property name="username" value="root"/>
          <property name="password" value="root"/>
        </dataSource>
      </environment>
   </environments>
   <mappers>
     <mapper resource="com/zrf/mybatis/config/sqlmap/UserMapper.xml"/>
   </mappers>
</configuration>


1.根据用户id查询用户

实体bean

public class User {
	private Integer id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public User() {
	}


	public User(Integer id, String username, Date birthday, String sex,
			String address) {
		super();
		this.id = id;
		this.username = username;
		this.birthday = birthday;
		this.sex = sex;
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday="
				+ birthday + ", sex=" + sex + ", address=" + address + "]";
	}

mapper配置文件

<?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">
  <select id="findUserById" parameterType="int" resultType="com.zrf.mybatis.bean.User">
     select * from user where id=#{id}
  </select>

</mapper>

测试类
public class Test {
  public static void main(String[] args) {
	String resource="com/zrf/mybatis/config/SqlMapConfig.xml";
	try {
		InputStream  input=Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(input);
		SqlSession sqlSession=sqlSessionFactory.openSession();
		User user=sqlSession.selectOne("user.findUserById", 24);
		System.out.println(user);
	} catch (IOException e) {
		e.printStackTrace();
	}
}


运行结果



2.根据用户名称模糊查询用户

实体bean
public class User {
	private Integer id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public User() {
	}


	public User(Integer id, String username, Date birthday, String sex,
			String address) {
		super();
		this.id = id;
		this.username = username;
		this.birthday = birthday;
		this.sex = sex;
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday="
				+ birthday + ", sex=" + sex + ", address=" + address + "]";
	}

mapper文件

<?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">
   <select id="findUserByName" parameterType="java.lang.String" resultType="com.zrf.mybatis.bean.User">
      select * from user where username like '%${value}%'
   </select>
</mapper>
测试类

public class Test {

	public static void main(String[] args) {
        String resource="com/zrf/mybatis/config/SqlMapConfig.xml";
        try {
        	InputStream inputStream=Resources.getResourceAsStream(resource);
        	SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        	SqlSession  sqlSession =sqlSessionFactory.openSession();
        	List<User>userList=sqlSession.selectList("user.findUserByName", "小明");
        	System.out.println(userList);
        	sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

运行结果



3.修改用户

实体bean
public class User {
	private Integer id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public User() {
	}


	public User(Integer id, String username, Date birthday, String sex,
			String address) {
		super();
		this.id = id;
		this.username = username;
		this.birthday = birthday;
		this.sex = sex;
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday="
				+ birthday + ", sex=" + sex + ", address=" + address + "]";
	}

mapper配置文件

<?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">
	<update id="updateUser">
		update user set
		username=#{username},birthday=#{birthday},sex=#{sex},address=#{address}
		where id=#{id}
	</update>
</mapper>


测试类
public class Test {

	public static void main(String[] args) {
		 String resource="com/zrf/mybatis/config/SqlMapConfig.xml";
         try {
        	 InputStream inputStream=Resources.getResourceAsStream(resource);
        	 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        	 SqlSession sqlSession=sqlSessionFactory.openSession();
        	 User user=new User();
        	 user.setId(29);
        	 user.setUsername("王大军");
        	 user.setBirthday(new Date());
        	 user.setSex("2");
        	 user.setAddress("甘肃平凉");
        	 sqlSession.update("user.updateUser", user);
        	 sqlSession.commit();
        	 sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

4.删除用户

实体bean
public class User {
	private Integer id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public User() {
	}


	public User(Integer id, String username, Date birthday, String sex,
			String address) {
		super();
		this.id = id;
		this.username = username;
		this.birthday = birthday;
		this.sex = sex;
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday="
				+ birthday + ", sex=" + sex + ", address=" + address + "]";
	}

mapper配置文件

<?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">
   <delete id="deleteUser" parameterType="java.lang.Integer">
      delete from user where id=#{id}
   </delete>
</mapper>

测试类

public class Test {

	public static void main(String[] args) {
       String resource="com/zrf/mybatis/config/SqlMapConfig.xml";
       try {
    	   InputStream  inputStream =Resources.getResourceAsStream(resource);
    	   SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    	   SqlSession sqlSession =sqlSessionFactory.openSession();
    	   sqlSession.delete("user.deleteUser", 28);
    	   sqlSession.commit();
    	   sqlSession.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
	}

}



5.添加用户

实体bean
public class User {
	private Integer id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public User() {
	}


	public User(Integer id, String username, Date birthday, String sex,
			String address) {
		super();
		this.id = id;
		this.username = username;
		this.birthday = birthday;
		this.sex = sex;
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", birthday="
				+ birthday + ", sex=" + sex + ", address=" + address + "]";
	}

配置文件
<?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">
   <insert id="insertUser" parameterType="com.zrf.mybatis.bean.User">
      insert into user(username,birthday,sex,address)value(#{username},#{birthday},#{sex},#{address})
   </insert>
</mapper>

测试类
public class Test {

	public static void main(String[] args) {
        String resource="com/zrf/mybatis/config/SqlMapConfig.xml";
        try {
        	InputStream  inputStream=Resources.getResourceAsStream(resource);
        	SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        	SqlSession sqlSession=sqlSessionFactory.openSession();
        	User user=new User();
        	user.setUsername("王小军");
        	user.setBirthday(new Date());
        	user.setSex("1");
        	user.setAddress("河南郑州");
        	sqlSession.insert("user.insertUser", user);
        	sqlSession.commit();
        	sqlSession.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值