Mybatis初入门
jopen
10年前
mybatis的配置xml,实体类,dao。
configuration.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> <typeAliases> <!--给实体类起一个别名 blog --> <typeAlias type="com.demo.po.Blog" alias="blog" /> </typeAliases> <!--数据源配置 这块用 mysql数据库 --> <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://localhost:3306/jfinal_demo" /> <property name="username" value="root" /> <property name="password" value="123" /> </dataSource> </environment> </environments> <mappers> <!--blogMapper.xml装载进来 同等于把“dao”的实现装载进来 --> <mapper resource="com/demo/mybatis/blogMapper.xml" /> </mappers> </configuration>
dao (xxxxMapper.java)
// dao层 方法名要和xxxMapper.xml的select id对应起来 package com.demo.dao; import com.demo.po.Blog; public interface BlogMapper { public Blog find(String id); }
xxxxMapper.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"> <!--这块等于dao接口的实现 namespace必须和接口的类路径一样--> <mapper namespace="com.demo.dao.BlogMapper"> <!-- findById必须和接口中的方法名一样 返回一个Blog 就是刚才的别名 如果不弄别名要连类路径一起写 麻烦--> <select id="find" parameterType="HashMap" resultType="Blog"> select * from blog where id=#{id} </select> </mapper>
实体类
package com.demo.po; public class Blog { // 数据库字段 private int id; private String title; private String content; // 添加get set方法 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
Test(测试类)
package com.demo.test; import java.io.IOException; import java.io.Reader; 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 com.demo.dao.BlogMapper; import com.demo.po.Blog; public class Test { /** * 获得MyBatis SqlSessionFactory * SqlSessionFactory负责创建SqlSession,一旦创建成功,就可以用SqlSession实例来执行映射语句,commit,rollback,close等方法。 * @return */ private static SqlSessionFactory getSessionFactory() { SqlSessionFactory sessionFactory = null; // 这块跟你的mybatis配置路径,最好写全,我就报过错 String resource = "com/demo/configuration.xml"; try { sessionFactory = new SqlSessionFactoryBuilder().build(Resources .getResourceAsReader(resource)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return sessionFactory; } public static void main(String[] args) { SqlSession sqlSession = getSessionFactory().openSession(); BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class); Blog blog = blogMapper.find("2"); System.out.println(blog.getTitle()); } }