spring整合mybatis两种方法
第一种:使用spring中xml创建dao接口实现类用set注入给sqlsession赋值再调用相应方法
public interface StudentMapper {
public List<Student> selectStudents();
}
public class StudentMapperImpl implements StudentMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<Student> selectStudents() {
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
return mapper.selectStudents();
}
}
<?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.tang.dao.StudentMapper">
<select id="selectStudents" resultType="student">
select *
from student
</select>
</mapper>
public class Student {
private Integer id;
private String name;
private String address;
public Student() {
}
public Student(Integer id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="db.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/mysqll"/>-->
<!-- <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="mybatis-config.xml" />
</bean>
<!-- <bean id="studentMapper" class="com.tang.dao.StudentMapperImpl">-->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" />-->
<!-- </bean>-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="studentMapper" class="com.tang.dao.StudentMapperImpl">
<property name="sqlSession" ref="sqlSession" />
</bean>
</beans>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysqll
jdbc.username=root
jdbc.password=123456
<?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.tang.domain"/>
</typeAliases>
<mappers>
<!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
<package name="com.tang.dao"/>
</mappers>
</configuration>
第二种:使用继承SqlSessionDaoSupport类省去了set注入和配置(新增)
public interface StudentMapper {
public List<Student> selectStudents();
}
public class StudentMapperImpl extends SqlSessionDaoSupport implements StudentMapper {
@Override
public List<Student> selectStudents() {
StudentMapper mapper = getSqlSession().getMapper(StudentMapper.class);
return mapper.selectStudents();
}
}
<?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.tang.dao.StudentMapper">
<select id="selectStudents" resultType="student">
select *
from student
</select>
</mapper>
public class Student {
private Integer id;
private String name;
private String address;
public Student() {
}
public Student(Integer id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
public class App {
public static void main( String[] args ) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentMapper studentMapper = (StudentMapper) ac.getBean("studentMapper");
List<Student> students = studentMapper.selectStudents();
for(Student student : students) {
System.out.println(student);
}
}
}
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="db.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/mysqll"/>-->
<!-- <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="mybatis-config.xml" />
</bean>
<bean id="studentMapper" class="com.tang.dao.StudentMapperImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">-->
<!-- <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>-->
<!-- </bean>-->
<!-- <bean id="studentMapper" class="com.tang.dao.StudentMapperImpl">-->
<!-- <property name="sqlSessionTemplate" ref="sqlSession" />-->
<!-- </bean>-->
</beans>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysqll
jdbc.username=root
jdbc.password=123456
<?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.tang.domain"/>
</typeAliases>
<mappers>
<!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/>-->
<package name="com.tang.dao"/>
</mappers>
</configuration>