在applicationContext.xml配置sqlSessionFactory和数据源
<context:property-placeholder location="DataSource.properties">context:property-placeholder>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="mybatis/SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
bean>
public class Student {
private int id;
private String name;
private String sex;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + "]";
}
}
<mapper namespace="cn.csdn.sm.dao.StuDao">
<select id="findStuById" parameterType="int" resultType="cn.csdn.sm.po.Student">
SELECT * FROM Student WHERE id=#{value}
select>
<select id="findStuByName" parameterType="String" resultType="cn.csdn.sm.po.Student">
SELECT * FROM Student WHERE name LIKE '%${value}%'
select>
<insert id="addStu" parameterType="cn.csdn.sm.po.Student" >
INSERT INTO Student(name,sex) VALUE(#{name},#{sex})
insert>
<delete id="delStu" parameterType="int">
DELETE FROM Student WHERE id = #{value}
delete>
<update id="upStu" parameterType="cn.csdn.sm.po.Student">
UPDATE Student SET name = #{name}, sex = #{sex} where id = #{id}
update>
mapper>
public interface StuDao {
public Student findStuById(int id) throws Exception;
public List findStuByName(String name) throws Exception;
public void addStu(Student Stu) throws Exception;
public void delStu(int id) throws Exception;
public void upStu(Student Stu) throws Exception;
}
注:
- 不在使用构造的方式注入sqlSessionFactory,而是通过spring注入,在实现类里面要继承spring-maybatis整合包中的SqlSessionDaoSupport类。
- 通过this.getSqlSession()获得sqlSession对象
- 不用close
public class StuImp extends SqlSessionDaoSupport implements StuDao{
@Override
public Student findStuById(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
Student Stu = sqlSession.selectOne("cn.csdn.mybatis.dao.StuDao.findStuById",id);
return Stu;
}
@Override
public List findStuByName(String name) throws Exception {
SqlSession sqlSession = this.getSqlSession();
List Stus = sqlSession.selectList("cn.csdn.mybatis.dao.StuDao.findStuByName",name);
return Stus;
}
@Override
public void addStu(Student stu) throws Exception {
SqlSession sqlSession = this.getSqlSession();
sqlSession.insert("cn.csdn.mybatis.dao.StuDao.addStu",stu);
sqlSession.commit();
}
@Override
public void delStu(int id) throws Exception {
SqlSession sqlSession = this.getSqlSession();
sqlSession.delete("cn.csdn.mybatis.dao.StuDao.delStu",id);
sqlSession.commit();
}
@Override
public void upStu(Student stu) throws Exception {
SqlSession sqlSession = this.getSqlSession();
sqlSession.update("cn.csdn.mybatis.dao.StuDao.upStu",stu);
sqlSession.commit();
}
}
在applicationContext.xml中配置dao
<bean id="stuDao" class="cn.csdn.sm.dao.StuImp">
<property name="sqlSessionFactory" ref="sqlSessionFactory">property>
bean>
在SqlMapConfig.xml中配置dao
<mappers>
<mapper resource="mapper/StuMapper.xml"/>
mappers>
class StuImpTest {
private ApplicationContext applicationContext;
@BeforeEach
void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
@Test
void testFindStuById() throws Exception {
StuDao stuDao = (StuDao) applicationContext.getBean("stuDao");
Student student = stuDao.findStuById(1);
System.out.println(student);
}
}
public interface StuMapper {
public Student findStuById(int id) throws Exception;
public List findStuByName(String name) throws Exception;
public void addStu(Student Stu) throws Exception;
public void delStu(int id) throws Exception;
public void upStu(Student Stu) throws Exception;
}
<mapper namespace="cn.csdn.sm.mapper.StuMapper">
<select id="findStuById" parameterType="int" resultType="cn.csdn.sm.po.Student">
SELECT * FROM Student WHERE id=#{value}
select>
<select id="findStuByName" parameterType="String" resultType="cn.csdn.sm.po.Student">
SELECT * FROM Student WHERE name LIKE '%${value}%'
select>
<insert id="addStu" parameterType="cn.csdn.sm.po.Student" >
INSERT INTO Student(name,sex) VALUE(#{name},#{sex})
insert>
<delete id="delStu" parameterType="int">
DELETE FROM Student WHERE id = #{value}
delete>
<update id="upStu" parameterType="cn.csdn.sm.po.Student">
UPDATE Student SET name = #{name}, sex = #{sex} where id = #{id}
update>
mapper>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.csdn.ssm.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
bean>
此时,SqlMapConfig.xml中也不用扫描dao包了
class StuMapperTest {
private ApplicationContext applicationContext;
@BeforeEach
void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
}
@Test
void testFindStuById() throws Exception {
StuMapper stuMapper = (StuMapper) applicationContext.getBean("stuMapper");
Student student = stuMapper.findStuById(1);
System.out.println(student);
}
}