1.准备工作
1.1 准备数据库
数据库1.2 准备与数据库表对应的pojo类
User:
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
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
+ ", birthday=" + birthday + ", address=" + address + "]";
}
}
2.配置文件
2.1 mybaits配置文件
sqlMapConfig.xml:
引入了所有的pojo类对应的xml mapping配置文件
<?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>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybaits?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- Mapper的位置 Mapper.xml 写Sql语句的文件的位置 (resource、class、url只能有一个,不能同时存在)-->
<mappers>
<mapper resource="mybaits/sqlmap/User.xml"/>
<!-- <mapper resource="sqlmap/User.xml" class="" url=""/> -->
<!-- <mapper class="com.itheima.mybatis.mapper.UserMapper" /> -->
<!-- <mapper url="" /> -->
<!--<package name="mybaits.sqlmap"/>-->
</mappers>
</configuration>
2.2 pojo类对应的mapping配置文件
User.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">
<!--命名空间:可以防止多个mapping文件都有同一个方法时的混乱问题,通过命名空间.方法名解决,比如user.findUserById-->
<mapper namespace="user">
<!--parameterType:入参类型,限制调用者使用时需要传参user.findUserById(int)-->
<!--resultType:返回值类型,调用者调用user.findUserById(int)这个方法以后会返回一个User对象-->
<select id="findUserById" parameterType="Integer" resultType="mybaits.pojo.User">
<!--#{}是mybaits占位符,jdbc占位符是?。-->
<!--value是引用传进来的inter值,这个名称任意,可以叫value,也可写任意字符-->
SELECT * FROM user WHERE id = #{value}
</select>
</mapper>
3 调用
package mybaits.junit;
import mybaits.pojo.User;
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 java.io.IOException;
import java.io.InputStream;
public class Test {
@org.junit.Test
public void testMybaits() throws IOException {
//加载核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行Sql语句
User user = sqlSession.selectOne("user.findUserById", 10);
System.out.println(user);
}
}
4 其他查询
4.1 用户名模糊查询
mapping:
<!--调用这希望的返回值是一个list<User>,但是这边resultType只需要写List的形参User即可-->
<select id="findUserByUsername" parameterType="String" resultType="mybaits.pojo.User">
<!-- 这里的value只可以写value,不能写成其他的值 -->
<!-- ${value}指代的就是传进来的username,比如张三,这里就是SELECT * FROM user WHERE username LIKE '%张三%'
而如果是写#{value}的话,这里就变成了SELECT * FROM user WHERE username LIKE '%'张三'%'-->
SELECT * FROM user WHERE username LIKE '%${value}%'
</select>
调用者:
@org.junit.Test
public void testMybaits2() throws IOException {
//加载核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行Sql语句
List<User> list = sqlSession.selectList("user.findUserByUsername", "张三");
for (User user:list) {
System.out.println(user);
}
}
4.2 插入数据
mapping:
<insert id="insertUser" parameterType="mybaits.pojo.User">
<!--只要username这些和User类里面的字段匹配好,虽然传进来的是mybaits.pojo.User,但是可以直接#{username}这样引用-->
INSERT INTO user (username,birthday,address,sex) VALUES (#{username},#{birthday},#{address},#{sex})
</insert>
调用者:
@org.junit.Test
public void testMybaits3() throws IOException {
//加载核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setSex("男");
user.setUsername("California");
//执行Sql语句
int result = sqlSession.insert("user.insertUser", user);
System.out.println(result);
//如果不commit就不可以插入成功
sqlSession.commit();
}
4.3 更新数据
mapping:
<update id="updateUserById" parameterType="mybaits.pojo.User">
UPDATE user SET username = #{username} WHERE id = #{id}
</update>
调用者:
@org.junit.Test
public void testMybaits4() throws IOException {
//加载核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setId(31);
user.setSex("男");
user.setUsername("tom");
//执行Sql语句
int result = sqlSession.update("user.updateUserById", user);
//如果不commit就不可以插入成功
sqlSession.commit();
System.out.println(result);
}
4.4 删除数据
mapping:
<delete id="deleteUserById" parameterType="mybaits.pojo.User">
DELETE FROM user WHERE id = #{id}
</delete>
调用者:
@org.junit.Test
public void testMybaits5() throws IOException {
//加载核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setId(30);
user.setSex("男");
user.setUsername("tom");
//执行Sql语句
int result = sqlSession.delete("user.deleteUserById", user);
//如果不commit就不可以插入成功
sqlSession.commit();
System.out.println(result);
}
网友评论