mybatis的使用
Posted jas0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis的使用相关的知识,希望对你有一定的参考价值。
简单的介绍一下mybatis的使用
采用的是maven的方式
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency>
一、mybatis-config.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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://xxxxxxxxxx:3306/test?characterEncoding=UTF-8"/> <property name="username" value="xxxxx"/> <property name="password" value="xxxxx"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mybatisMapping/studentMapper.xml"/> </mappers> </configuration>
二、bean的书写
package com.jas.mybatis.bean; public class StudentPO { private Integer id; private Integer courseId; private Integer studentId; private Integer ceId; private Integer addtime; private Integer modtime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getCourseId() { return courseId; } public void setCourseId(Integer courseId) { this.courseId = courseId; } public Integer getStudentId() { return studentId; } public void setStudentId(Integer studentId) { this.studentId = studentId; } public Integer getCeId() { return ceId; } public void setCeId(Integer ceId) { this.ceId = ceId; } public Integer getAddtime() { return addtime; } public void setAddtime(Integer addtime) { this.addtime = addtime; } public Integer getModtime() { return modtime; } public void setModtime(Integer modtime) { this.modtime = modtime; } }
三、beanMapper的书写
<?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.jas.mybatis.dao.IStudentDAO"> <select id="selectByCourseId" resultType="com.jas.mybatis.bean.StudentPO"> SELECT * FROM test.student WHERE course_id = #{courseId} </select> </mapper>
四、dao的书写
package com.jas.mybatis.dao; import com.jas.mybatis.bean.StudentPO; import org.apache.ibatis.annotations.Param; import java.util.List; public interface IStudentDAO { List<StudentPO> selectByCourseId(@Param("courseId") Integer courseId); }
五、test的运行
package com.jas.mybatis.test; import com.alibaba.fastjson.JSONObject; import com.jas.mybatis.bean.StudentPO; import com.jas.mybatis.dao.IStudentDAO; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.util.List; public class test1 { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); try { IStudentDAO studentDAO = sqlSession.getMapper(IStudentDAO.class); List<StudentPO> productList = studentDAO.selectByCourseId(10); System.out.println(JSONObject.toJSONString(productList)); } finally { sqlSession.close(); } } }
以上是关于mybatis的使用的主要内容,如果未能解决你的问题,请参考以下文章
MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段
[mybatis]动态sql_sql_抽取可重用的sql片段