Spring整合MyBatis(笔记总结)

需要用到的jar包

在这里插入图片描述

建如下包和类以及xml

在这里插入图片描述

mybatisconfig.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.woniu.entity"/>
	</typeAliases>
	<!-- jdbc参数由spring管理 -->

	<mappers>
		<package name="com.woniu.dao"/>
	</mappers>
</configuration>

applicationContext.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"
    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/context 
        http://www.springframework.org/schema/context/spring-context.xsd"> 
 		<!-- 配置sqlSessionFactory -->
 		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 			<!-- 设置MyBatis主配置文件的位置 -->
 			<property name="configLocation" value="classpath:mybatisconfig.xml" />
 			<!-- 设置数据源 -->
 			<property name="dataSource" ref="dataSource" />
 		</bean>	
 		<!-- 配置数据源(c3p0数据库连接池参数) -->
 		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 			<property name="driverClass" value="${jdbc.driver}" />
			<property name="jdbcUrl" value="${jdbc.url}" />
			<property name="user" value="${jdbc.username}" />
			<property name="password" value="${jdbc.password}" />
 		</bean>
 		<!-- 指定四大参数properties文件的位置 -->
 		<context:property-placeholder location="classpath:jdbc.properties" />
 		<!-- 获取SqlSession
 			获取Dao接口实现类对象  sqlSession.getMapper()
 			MapperScannerConfigurer:自动扫描指定包(dao接口所在的包)
 			自动生成所有dao接口的实现类,对应bean的id:接口是StudentDAO,接口的实现类id是:studentDAO
 		 -->
 		 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		 	<property name="basePackage" value="com.woniu.dao"/>
 		 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 		 </bean>
 		 
 		 <!-- 给service层dao对象赋值 -->
 		 <bean id="studentService" class="com.woniu.service.imp.StudentServiceImp">
 		 	<property name="studentDao" ref="studentDAO"/>
 		 </bean>
</beans>

Student.java

public class Student {
	private Integer sid;
	private String sname;
	private Integer sage;
	private Character ssex;
	private Integer cid;
	
	public Student() {
		super();
	}
	
	public Student(String sname, Integer sage, Character ssex, Integer cid) {
		super();
		this.sname = sname;
		this.sage = sage;
		this.ssex = ssex;
		this.cid = cid;
	}

	public Student(Integer sid, String sname, Integer sage, Character ssex, Integer cid) {
		super();
		this.sid = sid;
		this.sname = sname;
		this.sage = sage;
		this.ssex = ssex;
		this.cid = cid;
	}
	public Integer getSid() {
		return sid;
	}
	public void setSid(Integer sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public Integer getSage() {
		return sage;
	}
	public void setSage(Integer sage) {
		this.sage = sage;
	}
	public Character getSsex() {
		return ssex;
	}
	public void setSsex(Character ssex) {
		this.ssex = ssex;
	}
	public Integer getCid() {
		return cid;
	}
	public void setCid(Integer cid) {
		this.cid = cid;
	}
	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + ", sage=" + sage + ", ssex=" + ssex + ", cid=" + cid + "]";
	}
	
}

StudentDAO.java

public interface StudentDAO {
	void insertStudent(Student student);
}

StudentDAO.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.woniu.dao.StudentDAO">
	<insert id="insertStudent">
		insert into student values(null,#{sname},#{sage},#{ssex},#{cid},0)
	</insert>
</mapper>

StudentService.java

public interface StudentService {
	void addStudent(Student student);
}

StudentServiceImp.java

public class StudentServiceImp implements StudentService{
	//配置文件注入,完成初始化
	StudentDAO studentDao;
	
	public void setStudentDao(StudentDAO studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public void addStudent(Student student) {
		studentDao.insertStudent(student);
	}

}

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///infobase
jdbc.username=root
jdbc.password=123456

StudentServiceImpTest.java

public class StudentServiceImpTest {
	StudentService studentService;
	@Before
	public void setUp() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		studentService = (StudentService) ac.getBean("studentService");
	}
	@Test
	public void testInsertStudent() {
		Student student = new Student("张三",20,'男',2);
		studentService.addStudent(student);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值