- 查询所有用户
- SqlMappingConfig配置如下
<?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">
<!--使用jdbc事务管理-->
<transactionManager type="JDBC"/>
<!--配置数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/development"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--加载映射文件-->
<mappers>
<mapper resource="demo1/Mapping"/>
</mappers>
</configuration>
- 在Mapping中添加查询所有用户的sql语句
<?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="myTest">
<!--根据id查找用户-->
<select id="queryCustomerById" parameterType="Integer" resultType="demo1.Customer">
SELECT * FROM customer WHERE cust_id=#{cust_id}
</select>
<!--查询所有用户-->
<select id="queryAll" resultType="demo1.Customer">
SELECT * FROM customer;
</select>
</mapper>
- 编写测试类
@Test
public void test1() throws IOException {
// 1、创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//2、加载SqlMappingConfig配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMappingConfig");
//3、创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
//4、创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//5、执行sql查询
//参数queryAll为Mapping.xml的statement的id
List<Customer> customerList=sqlSession.selectList("queryAll");
//6、打印结果
for (Customer customer:customerList) {
System.out.println(customer);
}
//7、关闭连接,释放资源
sqlSession.close();
}
运行结果
- 根据用户名模糊查询用户
方式1:在Mapping中添加查询sql语句
<?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="myTest">
<!--根据id查找用户-->
<select id="queryCustomerById" parameterType="Integer" resultType="demo1.Customer">
SELECT * FROM customer WHERE cust_id=#{cust_id}
</select>
<!--查询所有用户-->
<select id="queryAll" resultType="demo1.Customer">
SELECT * FROM customer;
</select>
<!--根据用户名模糊查询用户-->
<select id="queryByName" resultType="demo1.Customer" parameterType="String">
SELECT * FROM customer WHERE cust_name LIKE '%${value}%'
</select>
</mapper>
编写测试类
// 根据用户名模糊查询用户
@Test
public void test2() throws IOException {
// 1、创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//2、加载SqlMappingConfig配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMappingConfig");
//3、创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
//4、创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//5、执行sql查询
//参数queryAll为Mapping.xml的statement的id
List<Customer> customerList=sqlSession.selectList("queryByName","三");
//6、打印结果
for (Customer customer:customerList) {
System.out.println(customer);
}
//7、关闭连接,释放资源
sqlSession.close();
}
运行结果
方式2:在Mapping中添加查询sql语句
<?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="myTest">
<!--根据id查找用户-->
<select id="queryCustomerById" parameterType="Integer" resultType="demo1.Customer">
SELECT * FROM customer WHERE cust_id=#{cust_id}
</select>
<!--查询所有用户-->
<select id="queryAll" resultType="demo1.Customer">
SELECT * FROM customer;
</select>
<!--根据用户名模糊查询用户-->
<select id="queryByName" resultType="demo1.Customer" parameterType="String">
SELECT * FROM customer WHERE cust_name LIKE #{cust_name}
</select>
</mapper>
编写测试类
// 根据用户名模糊查询用户
@Test
public void test2() throws IOException {
// 1、创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//2、加载SqlMappingConfig配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("SqlMappingConfig");
//3、创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
//4、创建SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//5、执行sql查询
//参数queryAll为Mapping.xml的statement的id
List<Customer> customerList=sqlSession.selectList("queryByName","%四%");
//6、打印结果
for (Customer customer:customerList) {
System.out.println(customer);
}
//7、关闭连接,释放资源
sqlSession.close();
}
运行结果
- #{}与${}的区别
#{}:
- 表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值
- 自动进行Java类型和jdbc类型转换
- #{}可以有效防止sql注入
- #{}可以接收简单类型值或pojo属性值
- 如果parameterType传输单个简单类型值,#{}括号中可以是value或其它名称
${}:
- 表示拼接sql串
- 通过${}可以将parameterType传入的内容拼接在sql中且不进行jdbc类型转换
- ${}可以接收简单类型值或pojo属性值
- 如果parameterType传输单个简单类型值,${}括号中只能是value
- sql语句详解
- parameterType:指定输入参数类型,Mybatis通过ognl从输入对象中获取参数值拼接在sql中
- resultType: 指定输出结果类型,Mybatis将sql查询结果的一行记录数据映射为resultType指定类型的对象,如果有多条数据,则分别进行映射,并把对象放到List容器中
- selectOne:查询一条记录,如果查询结果有多条记录则会抛出异常
- selectList:查询一条或多条记录