Mybatis使用
1. 第一个Mybatis程序 Mybatis-01
1.1 mybatis-config编写
<?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="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments>
<mappers>
<mapper resource="com/zhx/mapper/UserMapper.xml"/>
</mappers>
</configuration>
1.2 工具类
package com.zhx.utils; 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 MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "mybatis-config.xml"; InputStream inputstream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputstream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
1.3 实体类 (对照数据库创建)
package com.zhx.pojo; public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
1.4 mapper接口
package com.zhx.mapper; import com.zhx.pojo.User; import java.util.List; public interface UserMapper { List<User> getUserList(); }
1.5 对应的 xml配置文件 (代替原生方式的实现类)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zhx.mapper.UserMapper"> <!--配置文件必须写全限定名 --> <select id="getUserList" resultType="com.zhx.pojo.User"> select * from user </select> </mapper>
1.6 测试
package com.zhx.mapper; import com.zhx.pojo.User; import com.zhx.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserMapperTest { @Test public void Test1(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); // 方式一 UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUserList(); // 方式二 List<User> userList1 = sqlSession.selectList("com.zhx.mapper.UserMapper.getUserList"); for (User user : userList1) { System.out.println(user); } sqlSession.close(); } }
2. Spring整合mybatis Mybatis-02 通过mybatis-spring包来整合
2.1 实体类
package com.zhx.pojo; public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
2.2 UserMapper
package com.zhx.mapper; import com.zhx.pojo.User; import java.util.List; public interface UserMapper { public List<User> getUserList(); }
2.3 UserMapperImpl 和 UserMapperImpl_1
有继承 SqlSessionDaoSupport类的方法 和类 里面设置 SqlSessionTemplate属性两种方法
package com.zhx.mapper; import com.zhx.pojo.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserMapperImpl implements UserMapper{ // 之前我们所有的操作都是用sqlSession来执行,现在都是用SqlSessionTemplate private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } @Override public List<User> getUserList() { UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class); return mapper.getUserList(); } }
public class UserMapperImpl_1 extends SqlSessionDaoSupport implements UserMapper { @Override public List<User> getUserList() { SqlSession sqlSession = getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); return mapper.getUserList(); } }
2.4 UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.zhx.mapper.UserMapper"> <select id="getUserList" resultType="com.zhx.pojo.User"> select * from user </select> </mapper>
2.5 mybatis-config.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> <typeAliases> <package name="com.zhx.pojo"/> </typeAliases> <!-- <mappers>--> <!-- <mapper resource="com/zhx/mapper/UserMapper.xml"/>--> <!-- </mappers>--> </configuration>
2.4 spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 定义一个使用DBCP实现的数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:mapper/*"/> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <bean id="UserMapper" class="com.zhx.mapper.UserMapperImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> <!-- 因为UserMapperImpl_1继承了SqlSessionDaoSupport类,而SqlSessionDaoSupport类中有一个关于sqlSessionFactory的set方法 所以这里直接注入sqlSessionFactory,SqlSessionDaoSupport类中又提供了getSqlSession方法--> <bean id="UserMapper2" class="com.zhx.mapper.UserMapperImpl_1"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
3. 配置dao接口扫描包,动态的实现了dao接口可以注入到spring容器中。。不再需要实现类
注意:
<!-- 配置Mapper接口扫描包,动态的实现了Mapper接口可以注入到Spring容器中! 配置 此项后不再需要sqlSessionTemplate --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.zhx.mapper"/> </bean>