一、创建工程和数据库
1).工程名:ibatisdemo1
2).数据库名:ibatisdemo1
创建表:student
CREATE TABLE `student` (
`sid` int(11) NOT NULL,
`sname` varchar(30) DEFAULT NULL,
`major` varchar(30) DEFAULT NULL,
`birth` date DEFAULT NULL,
`score` decimal(10,0) DEFAULT NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
添加测试数据
insert into
`student`(`sid`,`sname`,`major`,`birth`,`score`)
values (1,'ss','ff','2014-03-06','22');
insert into
`student`(`sid`,`sname`,`major`,`birth`,`score`)
values (2,'vv','ee','2014-03-05','33');
二、添加相关jar
lib
mysql-connector-java.jar
ibatis-2.3.3.720.jar
junit-4.4.jar
三、添加配置文件
1.在项目中创建conf目录
/conf
2.在conf下添加jdbc属性文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=root
3.在conf下添加配置文件sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- 加载连接数据属性文件 -->
<properties resource="SqlMap.properties"/>
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}"/>
<property name="JDBC.ConnectionURL" value="${url}"/>
<property name="JDBC.Username" value="${username}"/>
<property name="JDBC.Password" value="${password}"/>
</dataSource>
</transactionManager>
</sqlMapConfig>
四、创建与数据库表中相关的javabean和映射文件
1.类名:Student.java
public class Student {
private Integer sid;
private String sname;
private String major;//主修专业
private Date birth;
private float socre;
}
2.映射文件名:Student.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias alias="Student" type="cn.jbit.domain.Student"/>
<!-- 查询所有 -->
<select id="selectAllStudent" resultClass="Student">
SELECT
*
FROM
student
</select>
<!-- 添加 -->
<insert id="insertStudent" parameterClass="Student">
INSERT INTO
Student(sid,sname,major,birth,score)
VALUES
(#sid#,#sname#,#major#,#birth#,#score#)
</insert>
<!-- 删除 -->
<delete id="deleteStudentById" parameterClass="int">
DELETE
FROM student
WHERE
sid=#sid#
</delete>
<!-- 修改 -->
<update id="updateStudent" parameterClass="Student">
UPDATE
student
SET
sname=#sname#,
score=#score#,
major=#major#,
birth=#birth#
WHERE
sid=#sid#
</update>
</sqlMap>
3.在核心配置文件 SqlMapConfig.xml添加引用映射文件
<!-- 加载映射文件 -->
<sqlMap resource="cn/jbit/domain/Student.xml"/>
五、设计DAO层
接口:IStudentDao.java
public interface IStudentDao {
/**
* 保存
* @param student
*/
public void save(Student student);
/**
* 根据id删除
* @param id
*/
public void delete(int id);
public void update(Student student);
/**
* 查询所有
* @return
*/
public List<Student> select();
}
实现类:StudentDaoImpl.java
public class StudentDaoImpl implements IStudentDao{
private static SqlMapClient sqlMapClient;
static{
try {
//加载配置文件
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
//实例化SqlMapClient
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
//关闭
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void delete(int id) {
try {
sqlMapClient.delete("deleteStudentById", id);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void save(Student student) {
try {
sqlMapClient.insert("insertStudent", student);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 查询所有学生信息
*/
@Override
public List<Student> select() {
List<Student> students=null;
try {
students = sqlMapClient.queryForList("selectAllStudent");
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
@Override
public void update(Student student) {
try {
sqlMapClient.update("updateStudent", student);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
六、设计SERVICE层
接口:IStudentService.java
public interface IStudentService {
/**
* 查询所有
* @return
*/
public List<Student> findAll();
/**
* 添加学生
* @param student
*/
public void save(Student student);
/**
* 根据id删除
* @param id
*/
public void delete(int id);
/**
* 修改
* @param student
*/
public void update(Student student);
}
实现类:StudentServiceImpl.java
public class StudentServiceImpl implements IStudentService {
private IStudentDao studentDao = new StudentDaoImpl();
@Override
public List<Student> findAll() {
return studentDao.select();
}
@Override
public void save(Student student) {
studentDao.save(student);
}
@Override
public void delete(int id) {
studentDao.delete(id);
}
@Override
public void update(Student student) {
studentDao.update(student);
}
}
七、测试
public class StudentTest {
IStudentService studentService = new StudentServiceImpl();
/**
* 查询所有
*/
@Test
public void testSelect(){
List<Student> stus = studentService.findAll();
System.out.println(stus.size());
}
/**
* 保存
*/
@Test
public void testSave(){
Student stu = new Student();
stu.setSid(3);
stu.setSname("xx");
stu.setMajor("yy");
stu.setBirth(Date.valueOf("1988-05-09"));
stu.setScore(33);
studentService.save(stu);
}
/**
* 删除
*/
@Test
public void testDelete(){
studentService.delete(3);
}
/**
* 修改
*/
@Test
public void testUpdate(){
Student student = studentService.findById(2);
student.setScore(55);
studentService.update(student);
}
}