jdbcInfo.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/studentdb
jdbc.username=root
jdbc.password=root
Mybatis-Config.xml (这是mybatis总配置文件)
<?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>
<!-- 引入外部属性文件 -->
<properties resource="jdbcInfo.properties" />
<!-- 配置java类全路径的别名 -->
<typeAliases>
<typeAlias alias="Student" type="com.xy.pojo.Student" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="StudentMapper.xml" />
</mappers>
</configuration>
StudentMapper.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="mybatis.mapper">
<!-- 使用已经配置的java类全路径的别名Student -->
<resultMap type="Student" id="studentMap">
<id property="id" column="id" />
<result property="sname" column="name" />
<result property="spwd" column="pwd" />
<result property="sbirth" column="birth" />
</resultMap>
<!-- sql语句是针对表的操作,而resultMap说明了表和pojo的映射关系 -->
<select id="getAllStu" resultMap="studentMap">
select
id,name,pwd,birth from
tbl_student;
</select>
<select id="getModelStu" parameterType="int" resultMap="studentMap">
select
id,name,pwd,birth from
tbl_student where id=#{id};
</select>
<select id="getModelByName" parameterType="String" resultMap="studentMap">
select
id,name,pwd,birth from
tbl_student where name like "%"#{name}"%";
</select>
<insert id="insertStu" parameterType="Student">
insert into
tbl_student(name,pwd,birth)
values(#{sname},#{spwd},#{sbirth});
</insert>
<delete id="deleteStu" parameterType="int">
delete from tbl_student
where id=#{id};
</delete>
<update id="updateStu" parameterType="Student">
update
tbl_student
set
name=#{sname},pwd=#{spwd},birth=#{sbirth} where id=#{id};
</update>
</mapper>
StudentDao
package com.xy.dao;
import java.util.List;
import com.xy.pojo.Student;
public interface StudentDao
{
public List<Student> getAllStu();
public Student getModel(int id);
public void add(Student student);
public void delete(int id);
public void update(Student student);
public List<Student> getModelByName(String name); // 模糊查询
}
BasicDaoImpl
package com.xy.dao.impl;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class BasicDaoImpl
{
private static SqlSessionFactory sqlSessionFactory = null;
static
{
String resource = "Mybatis-Config.xml";
try
{
Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
catch (IOException e)
{
System.out.println("创建SqlSessionFactory实例失败");
}
}
public static SqlSessionFactory getSqlSessionFactory()
{
return sqlSessionFactory;
}
}
StudentDaoImpl
package com.xy.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.xy.dao.StudentDao;
import com.xy.pojo.Student;
public class StudentDaoImpl implements StudentDao
{
// 添加学生
public void add(Student student)
{
SqlSession sqlSession = BasicDaoImpl.getSqlSessionFactory().openSession();
try
{
sqlSession.insert("mybatis.mapper.insertStu", student);
sqlSession.commit();
}
finally
{
sqlSession.close();
}
}
// 删除学生
public void delete(int id)
{
SqlSession sqlSession = BasicDaoImpl.getSqlSessionFactory().openSession();
try
{
sqlSession.insert("mybatis.mapper.deleteStu", id);
sqlSession.commit();
}
finally
{
sqlSession.close();
}
}
// 获取所有学生
public List<Student> getAllStu()
{
SqlSession sqlSession = BasicDaoImpl.getSqlSessionFactory().openSession();
List<Student> students = null;
try
{
students = (List<Student>) sqlSession.selectList("mybatis.mapper.getAllStu");
}
finally
{
sqlSession.close();
}
return students;
}
// 根据id获取学生
public Student getModel(int id)
{
SqlSession sqlSession = BasicDaoImpl.getSqlSessionFactory().openSession();
Student stu = null;
try
{
stu = (Student) sqlSession.selectOne("mybatis.mapper.getModelStu", id);
}
finally
{
sqlSession.close();
}
return stu;
}
// 更新学生
public void update(Student student)
{
SqlSession sqlSession = BasicDaoImpl.getSqlSessionFactory().openSession();
try
{
sqlSession.insert("mybatis.mapper.updateStu", student);
sqlSession.commit();
}
finally
{
sqlSession.close();
}
}
// 根据名字模糊查询
public List<Student> getModelByName(String name)
{
SqlSession sqlSession = BasicDaoImpl.getSqlSessionFactory().openSession();
List<Student> students = null;
try
{
students=(List<Student>)sqlSession.selectList("mybatis.mapper.getModelByName", name);
}
finally
{
sqlSession.close();
}
return students;
}
}
}
pojo
package com.xy.pojo;
import java.util.Date;
public class Student
{
private int id;
private String sname;
private String spwd;
private Date sbirth;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getSname()
{
return sname;
}
public void setSname(String sname)
{
this.sname = sname;
}
public String getSpwd()
{
return spwd;
}
public void setSpwd(String spwd)
{
this.spwd = spwd;
}
public Date getSbirth()
{
return sbirth;
}
public void setSbirth(Date sbirth)
{
this.sbirth = sbirth;
}
}
Test
package com.xy.test;
import java.util.List;
import com.xy.dao.StudentDao;
import com.xy.dao.impl.StudentDaoImpl;
import com.xy.pojo.Student;
import com.xy.util.DateUtil;
public class Test
{
public static void main(String[] args)
{
StudentDao sdao = new StudentDaoImpl();
// 查找所有学生
System.out.print("查找所有学生\n");
List<Student> stus = sdao.getAllStu();
for (int i = 0; i < stus.size(); i++)
{
System.out.print(stus.get(i).getSname());
System.out.print(DateUtil.parseToString(stus.get(i).getSbirth(), DateUtil.yyyyMMdd));
System.out.print("\n");
}
// 根据id获取学生
System.out.print("根据id获取学生\n");
Student stu = sdao.getModel(1);
System.out.print(stu.getSname());
System.out.print(DateUtil.parseToString(stu.getSbirth(), DateUtil.yyyyMMdd));
System.out.print("\n");
// 添加学生
Student s = new Student();
s.setSname("xynew");
s.setSpwd("111");
String strBirth = "1990-02-28";
s.setSbirth(DateUtil.parseToDate(strBirth, DateUtil.yyyyMMdd));
sdao.add(s);
System.out.print("已经添加记录");
System.out.print("\n");
// 删除记录
// 更新记录
Student stuUpdate = new Student();
stuUpdate.setId(6);
stuUpdate.setSname("xy333");
stuUpdate.setSpwd("111");
String strBirthUpdate = "1990-02-28";
stuUpdate.setSbirth(DateUtil.parseToDate(strBirthUpdate, DateUtil.yyyyMMdd));
sdao.update(stuUpdate);
System.out.print("已经更新记录");
System.out.print("\n");
// 模糊查询
System.out.print("模糊查询\n");
List<Student> list = sdao.getModelByName("x");
for (int i = 0; i < list.size(); i++)
{
System.out.print(list.get(i).getSname());
System.out.print(DateUtil.parseToString(list.get(i).getSbirth(), DateUtil.yyyyMMdd));
System.out.print("\n");
}
}
}