发表于: 2018-01-30 21:59:37
1 606
今日完成
mybatis的学习
1.各种属性的作用;
2.mybatis的执行流程
3.连接数据库进行增改查等操作;
以下是进行查询的代码:
实体类
public class Student1 {
private int ID;
private String name;
private BigInteger QQ;
private String onlineID;
private String time_of_enrollment;
private String graduate_institutions;
private String report_link;
private String swear;
private String hearfrom;
//省略setter和getter;
@Override
public String toString() {
return "Student1{" +
"ID=" + ID +
", name='" + name + '\'' +
", QQ=" + QQ +
", onlineID='" + onlineID + '\'' +
", time_of_enrollment='" + time_of_enrollment + '\'' +
", graduate_institutions='" + graduate_institutions + '\'' +
", report_link='" + report_link + '\'' +
", swear='" + swear + '\'' +
", hearfrom='" + hearfrom + '\'' +
'}';
}
全局配置:
<?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://localhost:3306/wodeshujuku" />
<property name="username" value="root" />
<property name="password" value="w7217459" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="Student.xml" />
</mappers>
</configuration>
映射文件:
<?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="test">
<select id="findstudentbyID" parameterType="int" resultType="Estudiomybatis.Student1">
select ID,swear,hearfrom from student where id = #{id}
</select>
</mapper>
测试文件:
public class Test1 {
public SqlSession getSession() throws IOException{
String resource="SqlMapConfig.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();//
return sqlSession;
}
@Test
public void findstudentbyID() throws IOException{
SqlSession sqlSession=getSession();
Student1 p= sqlSession.selectOne("findstudentbyID", 1);
System.out.println(p);
sqlSession.close();
}
}
输出结果:
4.初步学习原始dao开发方法;
明日计划
学习mybatis;
遇到问题
-------
收获
学习了mybatis连接数据库及进行操作;
评论