项目结构:
1.pom.xml
<!-- ibatis.jar 从网络获取,则不需要手动导入ibatis的jar包!--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!-- junit.jar --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency>
2.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> <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/test?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <!-- 加载映射文件 --> <mappers> <mapper resource="Mapper/Student.xml" /> </mappers> </configuration>
3.Student.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.yanfang.Dao"> <resultMap id="StudentMap" type="com.yanfang.PO.Student"> <id property="id" column="id"></id> <result property="sId" column="s_id"></result> <result property="sName" column="s_name"></result> <result property="sBirth" column="s_birth"></result> <result property="sSex" column="s_sex"></result> </resultMap> <select id="selectStudentById" resultMap="StudentMap" > select * from student where id=#{id} </select> <select id="selectLike" resultMap="StudentMap"> select * from student where s_name like '${value}' </select> </mapper>
4.测试
import com.yanfang.Dao.StudentDao; import com.yanfang.PO.Student; 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 org.junit.Test; public class StudentDaoTest { @Test public void test1() throws IOException { // 核心配置文件 String resource = "SqlMapConfig.xml"; // 通过流将核心配置文件加载进来 InputStream inputStream = Resources.getResourceAsStream(resource); // 通过配置文件创建会话工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); // 通过会话工厂获取会话 SqlSession openSession = factory.openSession(); // 通过会话执行sql 第一个参数是名称空间+SqlID 第二个参数表示sql执行需要的参数 Student student = openSession.selectOne("com.yanfang.Dao.selectStudentById", 1); System.out.println(student.toString()); // 关闭会话 openSession.close(); } @Test public void test2() throws IOException { // 核心配置文件 String resource = "SqlMapConfig.xml"; // 通过流将核心配置文件加载进来 InputStream inputStream = Resources.getResourceAsStream(resource); // 通过配置文件创建会话工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); // 通过会话工厂获取会话 SqlSession openSession = factory.openSession(); // 通过会话执行sql 第一个参数是名称空间+SqlID 第二个参数表示sql执行需要的参数 Student student = openSession.selectOne("com.yanfang.Dao.selectLike", "李云"); System.out.println(student.toString()); // 关闭会话 openSession.close(); } }
或使用dao层
public interface StudentDao { Student selectStudentById(Integer id); }
StudentDao studentDao=openSession.getMapper(StudentDao.class); Student student = studentDao.selectStudentById(1);
5.小结
a.如果参数为简单类型是${}里的值必须为value。如果like 后要加引号时用$。order by ${column}
<select id="selectLike" resultMap="StudentMap"> select * from student where s_name like '${value}' </select>