项目目录结构
相关文件
db.properties
mysql.driver = com.mysql.jdbc.Driver
mysql.url = jdbc:mysql://localhost:3306/batis?characterEncoding=UTF-8
mysql.username = root
mysql.password = root
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>
<!--加载配置文件-->
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>
</environment>
</environments>
<!--映射文件的路径配置-->
<mappers>
<mapper resource="cn/zgx/batis/first/pojo/UserMapper.xml"></mapper>
</mappers>
</configuration>
User.java
public class User {
private int id;
private String username;
private int age;
private Date birthday;
//set和get方法省略了
//构造方法省略了
//toString方法省略了
}
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">
<!--如何实体类的字段名和数据库的列的值不一样,需要在这里配置一个映射,
property是pojo类的字段名,column是数据库对应的类的名,
是一样的话就不要了-->
<!-- <resultMap id="" type="">
<id property="" column=""></id>
<result property="" column=""></result>
<result property=""></result>
</resultMap>-->
<!-- parameterType:指定输入参数的类型-->
<!-- resultType:指定返回参数的类型-->
<select id="selectUserById" parameterType="int" resultType="cn.zgx.batis.first.pojo.User">
/*#{id}中的id可以换成如何合法的字符串*/
</select>
<!--#和$的区别:
1.#将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。
如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by "111",
如果传入的值是id,则解析成的sql为order by "id".
2. $将传入的数据直接显示生成在sql中。
如:order by $user_id$,如果传入的值是111,那么解析成sql时的值为order by user_id,
如果传入的值是id,则解析成的sql为order by id.
3.能用#就不要用$,$会引发sql注入的问题
-->
<select id="selectUserByUsername" parameterType="string" resultType="cn.zgx.batis.first.pojo.User">
SELECT * FROM USER WHERE username like '%${value}%'
/*${value}只能是value,不然会报错*/
</select>
</mapper>
FirstDemo.java
package cn.zgx.batis.first;
import cn.zgx.batis.first.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 org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class FirstDemo {
String resource = "SqlMapConfig.xml";
SqlSessionFactory sqlSessionFactory = null;
SqlSession session = null;
/*在test注解修饰的方法执行前执行*/
@Before
public void before() throws Exception{
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
}
/*如果使用junit时报错:Java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
* 原因是高版本的junit没有了hamcrest-core和hamcrest-library包。
* 解决:加入这两个jar包就行了
* */
@Test
public void testFindUserById() throws Exception{
User user = session.selectOne("test.selectUserById", 1);
System.out.println(user);
}
@Test
public void testFindUserByUsername() throws Exception{
/*test是UserMapper.xml中的namespace的值,selectUserByUsername是select标签的id值*/
List<User> list= session.selectList("test.selectUserByUsername", "小张");
System.out.println(list);
}
}
user表的sql语句
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`username` varchar(20) NOT NULL,
`age` int(3) DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
user表内容
id | username | age | birthday |
+—-+———-+—–+———————+
| 1 | zgx | 21 | 2016-10-04 13:37:37 |
| 2 | 小张 | 22 | 2017-08-01 13:54:53 |
| 3 | 小小张 | 23 | 2017-08-03 14:02:13
测试结果: