简单的实现查询
1.接口
package com.yyf.mapper;
import com.yyf.pojo.User;
import java.util.List;
public interface userMapper {
public List<User> selectUser();
}
2.接口配置文件
<?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.yyf.mapper.userMapper">
<select id="selectUser" resultType="user">
select * from mybatis.user
</select>
</mapper>
3.接口实现类
package com.yyf.mapper;
import com.yyf.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class userMapperImpl implements userMapper{
// 原来所有的操作都是由sqSession执行,现在由sqsSessionTemplate
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<User> selectUser() {
userMapper mapper = sqlSession.getMapper (userMapper.class);
return mapper.selectUser ();
}
}
3。mybatis 配置文件 里面只实现了别名的配置
<?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.yyf.pojo"/>
</typeAliases>
</configuration>
4.spring和mybatis整合配置文件
第一步 :datasource 数据源 连接数据库
第二步: 创建sqlsessionFactory
第三步:创建sqlsession
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/context/spring-aop.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--DataSource 使用Spring的数据源替换Mybatis的配置 c3p0 DBCP druid
我们这里用spring提供的jdbc org.springframework.jdbc.datasource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&charset=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 绑定mybatis配置文件-->
<property name="configLocation" value="classpath:mybait-config.xml"/>
<property name="mapperLocations" value="classpath:com/yyf/mapper/*Mapper.xml"/>
</bean>
<!-- sqlSession SqlSessionTemplate就是我们的SqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能使用构造去构造seqSession因为他没有set方法
引用注入 不能使用 value ,,,可以了
ref="dataSource"/
-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="usermapper1" class="com.yyf.mapper.userMapperImpl">
<!--<property name="sqlSession" value="sqlSession"/>-->
<property name="sqlSession" ref="sqlSession"/>
</bean>
<bean id="usermapper2" class="com.yyf.mapper.userMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
5.测试类
import com.yyf.mapper.userMapper;
import com.yyf.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.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyTest {
@Test
public void test() throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext ("mybatis-spring.xml");
userMapper usermapper = context.getBean ("usermapper2", com.yyf.mapper.userMapper.class);
for (User user : usermapper.selectUser ()) {
System.out.println (user);
}
}
}