warning:下面一切的內(nèi)容必需基于你的spring能正常使用秃励,不能使用的你還是先不要看氏仗。。莺治。zzz
一廓鞠、準(zhǔn)備工作##
- jar包下載
mybatis-3.4.4.jar
mybatis-spring-1.3.1.jar
spring-jdbc-4.2.4.RELEASE.jar
mysql-connector-Java-5.1.10.jar(由于我這里使用mysql為例)
spring-tx-4.2.4.RELEASE.jar - mysql建庫(kù)建表初始化數(shù)據(jù)
create database studentDB;
use studentDB;
create table student(id char(11) primary key,name varchar(30),age tinyint,sex char(1));
insert into student values('12014052074','xyh',21,'男');
insert into student values('1201405207x','xiaojiejie',18,'女');
二帚稠、定義bean類(lèi)和dao接口以及映射XML文件##
1谣旁、bean(student.java)
@Component//該處注解該bean為一個(gè)spring托管的bean
public class Student {
private String id;
private String name;
private byte age;
private String sex;
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 int getAge() {
return age;
}
public void setAge(byte age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
2、 dao(studentDao.java)
public interface StudentDao {
public int updateStudent(Student s);\\更新記錄接口方法
public List<Student> getAllStudent();\\獲取所有表行記錄
}
3滋早、XML(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.xyh.dao.StudentDao">
<update id="updateStudent" parameterType="com.xyh.beans.Student">
update student set name=#{name},age=#{age},sex=#{sex} where id=#{id}
</update>
<resultMap type="com.xyh.beans.Student" id="students">
<id column="id" property="id" jdbcType="CHAR"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="TINYINT" />
<result column="sex" property="sex" jdbcType="CHAR"/>
</resultMap>
<select id="getAllStudent" resultMap="students">
select * from student
</select>
</mapper>
mapper標(biāo)簽的namespace屬性為該映射文件對(duì)應(yīng)Dao接口類(lèi)榄审。
update標(biāo)簽用于更新操作,id屬性為對(duì)應(yīng)方法名杆麸,paramerType屬性為傳入方法的參數(shù)類(lèi)型搁进,標(biāo)簽體為操作sql,#{x}為傳入?yún)?shù)bean的x屬性昔头。
resultMap標(biāo)簽定義返回映射集饼问,由于select操作中返回的結(jié)果需要存儲(chǔ)為list集合,type屬性為集合中元素類(lèi)型揭斧,id標(biāo)簽對(duì)應(yīng)數(shù)據(jù)庫(kù)主鍵列莱革,column屬性為表中字段名,property為bean中屬性名,jdbcType為表字段類(lèi)型(注意:并不是與表中類(lèi)型名稱(chēng)都一樣)
select 標(biāo)簽用于查詢(xún)操作盅视,id屬性對(duì)應(yīng)方法名捐名,resultMap屬性為返回類(lèi)型,因?yàn)槭墙Y(jié)果集合闹击,使用前面定義的resultMap镶蹋。
4、mybatis.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>
<mappers>
<mapper resource="com/xyh/dao/StudentDao.xml"/>
</mappers>
</configuration>
引入定義的studentDao.xml文件
三赏半、配置spring.xml配置文件
1贺归、配置mysql數(shù)據(jù)源
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/newsDB?characterEncoding=UTF-8"></property>
<property name="username" value="root"></property>
<property name="password" value="12345"></property>
</bean>
2、配置會(huì)話(huà)工廠(chǎng)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>
<property name="dataSource" ref="dataSource" />
</bean>
3断箫、配置studentDao
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.xyh.dao.StudentDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
好了牧氮,該配置該定義該準(zhǔn)備的都完成了,讓我們來(lái)看看結(jié)果吧
四瑰枫、 test##
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext atc = new ClassPathXmlApplicationContext("spring.xml");
Student s = (Student) atc.getBean("student");
s.setId("12014052074");
s.setName("xyh");
s.setAge((byte)22);
s.setSex("男");
StudentDao dao = (StudentDao) atc.getBean("studentDao");
System.out.println(dao.updateStudent(s));
}
}
輸出結(jié)果為1(影響數(shù)據(jù)行數(shù))踱葛,這時(shí)候去數(shù)據(jù)庫(kù)一看,噢光坝,我又老了一歲了尸诽。。盯另。zzz
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext atc = new ClassPathXmlApplicationContext("spring.xml");
StudentDao dao = (StudentDao) atc.getBean("studentDao");
List<Student> students = dao.getAllStudent();
System.out.println(students.size());
}
}
輸出結(jié)果為2性含,數(shù)據(jù)集的數(shù)據(jù)size。
好了鸳惯,到這里你還整合不了的話(huà)~~~~~~~~~~~你還是從頭再來(lái)吧商蕴。。芝发。zzz