javaee框架-mybatis作业
使用Mapper接口动态代理的方式编程
//成功
查询user中id=2的用户
添加一条用户信息到user中(id=6)
删除user中id=6的用户信息
//未成功
修改user中id=4的用户的信息
模糊查询user中username带“小”的的用户信息(模糊查询)
目录结构
User.java
package cn.mybatis.bean;
public class User {
private int id;
private String username;
private String sex;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
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;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex
+ ", address=" + address + "]";
}
}
UserMapper.java
package com.mybatis.dao;
import cn.mybatis.bean.User;
//DAO接口
public interface UserMapper {
public User findUserById(int id) throws Exception;
public User findUserByName(String username) throws Exception;
public User updateUserById(User user) throws Exception;
public User insertUserInfo(User user)throws Exception;
public User deleteUserById(int id)throws Exception;
}
UserMapper.xml
<?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="com.mybatis.dao.UserMapper">
<!-- 根据id查询用户信息 -->
<select id="findUserById" parameterType="int" resultType="cn.mybatis.bean.User">
select * from user where id=#{id};
</select>
<insert id="insertUserInfo" parameterType="cn.mybatis.bean.User">
insert into user(id,username,sex,address)
values(#{id},#{username},#{sex},#{address});
</insert>
<!-- 根据姓名模糊查询 -->
<select id="findUserByName" parameterType="string" resultType="cn.mybatis.bean.User">
select * from user where username like '%${value}%'
</select>
<update id="updateUserById" parameterType="cn.mybatis.bean.User" >
update user set username=#{username},sex=#{sex},address=#{address} where id=#{id};
</update>
<delete id="deleteUserById" parameterType="int">
delete from user where id =#{id}
</delete>
</mapper>
SqlMapConfig.xml
<?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="com.mybatis.dao.UserMapper">
<!-- 根据id查询用户信息 -->
<select id="findUserById" parameterType="int" resultType="cn.mybatis.bean.User">
select * from user where id=#{id};
</select>
<insert id="insertUserInfo" parameterType="cn.mybatis.bean.User">
insert into user(id,username,sex,address)
values(#{id},#{username},#{sex},#{address});
</insert>
<!-- 根据姓名模糊查询 -->
<select id="findUserByName" parameterType="string" resultType="cn.mybatis.bean.User">
select * from user where username like '%${value}%'
</select>
<update id="updateUserById" parameterType="cn.mybatis.bean.User" >
update user set username=#{username},sex=#{sex},address=#{address} where id=#{id};
</update>
<delete id="deleteUserById" parameterType="int">
delete from user where id =#{id}
</delete>
</mapper>
Test.java
package com.mybatis.test;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import cn.mybatis.bean.User;
import com.mybatis.dao.UserMapper;
public class Test {
// SqlSessionFactory是线程安全的,这里可以使用单例模式
private static SqlSessionFactory sqlSessionFactory;
static {
try {
// 1、读取配置文件SqlMapConfig.xml
InputStream inputStream = Resources
.getResourceAsStream("SqlMapConfig.xml");
// 2、根据配置文件构建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
// Mapper接口动态生成代理类
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// findbyid
User user2 = userMapper.findUserById(2);
System.out.println(user2.toString());
// insert
User user3 = new User();
user3.setId(6);
user3.setUsername("郑铮铮");
user3.setSex("女");
user3.setAddress("3309");
userMapper.insertUserInfo(user3);
System.out.println("success");
//delete
userMapper.deleteUserById(6);
//未实现
//update
User user4 = new User();
user4.setId(4);
user4.setAddress("6666");
user4.setSex("男");
user4.setUsername("爸爸去哪儿");
userMapper.updateUserById(user4);
// findbyname
User user1 = userMapper.findUserByName("小");
System.out.println(user1.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sqlSession.commit();
sqlSession.close();
}
}