首先在項(xiàng)目上導(dǎo)入所需的jar:(后面有視頻資源,源碼和jar的下載)
mybatis_spring.png
然后是創(chuàng)建好bean,我這里是用Student為例
public class Student
{
//對應(yīng)上數(shù)據(jù)庫的信息
private String id;
private String name;
private String sex;
private String age;
public String getId() {
return id;
}
public void setId(String 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;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex
+ ", age=" + age + "]";
}
}
然后是配置student.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="Student.Message.IStudent.IStudent">
<resultMap type="Student.Message.bean.Student" id="Student">
<!-- 擁有主鍵的話就用ID標(biāo)簽 -->
<id column="ID" jdbcType="INTEGER" property="id"/>
<result column="NAME" jdbcType="VARCHAR" property="name"/>
<result column="SEX" jdbcType="VARCHAR" property="sex"/>
<result column="AGE" jdbcType="INTEGER" property="age"/>
</resultMap>
<select id="showStudent" parameterType="Student.Message.bean.Student" resultMap="Student">
select * from student
</select>
</mapper>
mybatis_spring1.png
接下來是student.xml各種sql的實(shí)現(xiàn)接口
public interface IStudent
{
//顯示全部學(xué)生信息
public List<Student> showStudent();
}
配置總的mybatis文件
<configuration>
<!-- 配置log4j文件 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<mappers>
<!-- Student.xml的全路徑 -->
<mapper resource="Student/Message/config/Student.xml"/>
</mappers>
</configuration>
配置spring文件
這里引用了外部的文件來配置連接數(shù)據(jù)庫
使用context的命名空間采章,注意:開頭的網(wǎng)址运嗜,是用來引入各種命名空間
//這是db.properties文件
user=root
password=root
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql://localhost:3306/test
<?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-3.0.xsd
">
<context:property-placeholder location="classpath:db.properties" />
<!--
獲取數(shù)據(jù)源
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource屬性指定要用到的連接池-->
<property name="dataSource" ref="dataSource"></property>
<!-- configLocation屬性連接mybatis的核心配置文件 -->
<property name="configLocation" value="Configuration.xml"></property>
</bean>
<bean id="showstudent" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--mapperInterface屬性指定映射器接口,用于實(shí)現(xiàn)此接口并生成映射器對象
value:接口的全類名
-->
<property name="mapperInterface" value="Student.Message.IStudent.IStudent"></property>
<!--sqlSessionFactory屬性指定要用到的SqlSessionFactory實(shí)例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
最后是測試類
public class Main
{
public static void main(String[] args)
{
//用來接收從數(shù)據(jù)
List<Student> studentlist=new ArrayList<Student>();
//創(chuàng)建IOC容器
//ClassPathXmlApplicationContext是ApplicationContext的實(shí)現(xiàn)類悯舟,從類路徑來加載配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.從ioc容器中獲取bean實(shí)例
//利用id來定位IOC容器中的bean
IStudent istudent=(IStudent) ctx.getBean("showstudent");
studentlist=istudent.showStudent();
for(Student s:studentlist)
{
System.out.println(s.toString());
}
}
}
結(jié)果成功實(shí)現(xiàn)
搜狗截圖17年07月20日1639_5.png
源碼下載(http://pan.baidu.com/s/1bKHLls) 密碼:jlv1
spring學(xué)習(xí)
mybatis學(xué)習(xí)
(http://pan.baidu.com/s/1i5n9ElR) 密碼為:u07n