myBatis的基本操作(持续更新中。。。)
目录
- 1. 简介
- 2. 简单使用
- 3. 代理开发
- 4. 小技巧
- 5. 动态查询
- 6. 注解(待更新)
- 底部
1. 简介
mybatis是一款优秀的持久层框架,用来简化JDBC开发
持久层:负责将数据保存到数据库的那一层代码
2. 简单使用
依赖
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.14</version> </dependency><!-- mysql的驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> <version>4.13.2</version> </dependency> </dependencies>
<?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="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers></configuration>
Userx
package com.yxz.pojo; import java.util.Date; public class Userx { private Integer id; private String username; private Date birthday; private Character 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 Character getSex() { return sex; } public void setSex(Character sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Userx{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", sex=" + sex + ", address='" + address + '\'' + '}'; }
}
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="test"> <select id="selectAll" resultType="com.yxz.pojo.Userx"> select * from userx; </select>
</mapper>
进行测试:
package com.yxz; import com.yxz.pojo.Userx;
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;
import java.util.List; public class MybatisDemo { public static void main(String[] args) throws IOException { // 1. 加载mybatis的核心配置文件,获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 2. 获取对应的SqlSession对象,用它执行sql SqlSession sqlSession = sqlSessionFactory.openSession(); // 3. 执行sql List<Userx> userxs = sqlSession.selectList("test.selectAll"); System.out.println(userxs); sqlSession.close(); }
}
建好数据库
目录:
3. 代理开发
创建一个接口,作为代理
package com.yxz.dao; import com.yxz.pojo.Userx; import java.util.List; public interface UserMapper { List<Userx> selectAll();
}
命名空间修改:
<?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.yxz.dao.UserMapper"> <select id="selectAll" resultType="com.yxz.pojo.Userx"> select * from userx; </select>
</mapper>
进行测试
package com.yxz; import com.yxz.dao.UserMapper;
import com.yxz.pojo.Userx;
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;
import java.util.List; public class MybatisDemo { public static void main(String[] args) throws IOException { // 1. 加载mybatis的核心配置文件,获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 2. 获取对应的SqlSession对象,用它执行sql SqlSession sqlSession = sqlSessionFactory.openSession(); // 3. 执行sql
// List<Userx> userxs = sqlSession.selectList("test.selectAll"); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<Userx> userxs = userMapper.selectAll(); System.out.println(userxs); sqlSession.close(); }
}
4. 小技巧
起别名
<typeAliases> <package name="com.yxz.pojo"/>
</typeAliases>
<select id="selectAll" resultType="userx"> select <include refid="brand_column"/> from userx;
</select>
<sql id="brand_column"> id,username, birthday, sex, address
</sql>
<select id="selectById" parameterType="int" resultType="userx"> select <include refid="brand_column"/> from userx where id <![CDATA[ < ]]> #{id};</select>
5. 动态查询
choose when otherwise
set if
delete from user where id
in(<foreach collction="ids" item="id" separator=",">#{id} </foreach>) ;
6. 注解(待更新)
@Select
@Insert
@Update
@Delete