mybatis是一个轻量级的持久层框架,这里记录下mybatis的入门级应用。废话不多说,下面是简单的使用流程:
1.引入jar包mybatis-3.4.5.jar(引入数据库连接工具包mysql-connector-java-5.1.39-bin.jar)
2.创建mybatis配置文件mybatis-config.xml,文件标签结构可参考:http://www.mybatis.org/mybatis-3/getting-started.html
<?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="mydb.property"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${mydb.driver}"/> <property name="url" value="${mydb.url}"/> <property name="username" value="${mydb.username}"/> <property name="password" value="${mydb.password}"/> </dataSource> </environment> </environments> <mappers> <mapper class="cn.zjl.model.StudentMapper"/> </mappers> </configuration>
3、创建数据库连接配置文件:mydb.property
mydb.driver=com.mysql.jdbc.Driver mydb.url=jdbc:mysql://localhost/ssm mydb.username=root mydb.password=123456
4、新建实体类,mapper接口、mapper.xml(数据库添加student表及对应字段)
4.1 实体类Student包含属性id、name、age、gender,及get、set方法
4.2 mapper接口,定义如下方法:
selectOne(int id)根据id查询一条记录
selectAll()查询全部记录。
insertStu(Student student)添加一条记录。
updateStu(Student student)更新一条记录。
deleteOne(int id)删除一条记录。
4.3 mapper.xml定义数据库操作的sql语句。
<?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="cn.zjl.model.StudentMapper"> <select id="selectOne" resultType="cn.zjl.model.Student"><!-- id值必须同mapper中的方法名一样 --> select * from Student where id = #{id} </select> <select id="selectAll" resultType="cn.zjl.model.Student"> select * from Student </select> <insert id="insertStu"> insert into Student(name, age, gender) values (#{name}, #{age}, #{gender}) </insert> <update id="updateStu"> update Student set name=#{name}, age=#{age} where id=${id} </update> <delete id="deleteOne"> delete from Student where id=#{id} </delete> </mapper>
5、mybatis的session管理工具类SessionUtil:
public class SessionUtil { public static SqlSession openSession() throws IOException{ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource);//读取mybatis配置文件 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//获取session工厂对象 return sqlSessionFactory.openSession();//返回一个session实例 } }
6、测试mybatis项目的效果MybatisTest:
6.1 项目里右键新建一个junit test case,取名为MybatisTest;
6.2 新建测试方法:insertOne()
@Test public void insertOne() throws IOException { SqlSession session = SessionUtil.openSession();// 通过SessionUtil获取session StudentMapper mapper = session.getMapper(StudentMapper.class);// 获取操作数据的工具类对象 Student student = new Student(); // student.setAge(15); student.setName("ali"); student.setGender("F"); mapper.insertStu(student); session.commit(); // 提交session session.close(); // 关闭session /*List<Student> list = mapper.selectAll(); System.out.println(list); Student s1 = new Student(); s1.setId(1); s1.setName("mmmmm"); mapper.updateStu(s1); List<Student> l1 = mapper.selectAll(); System.out.println(l1);*/ }
测试结果不在赘述。