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"><!--通过这个配置文件,完成mybatis与数据库的连接 --><configuration> <!-- 注意此配置文件内的元素的 --> <!-- 引入database.properties文件 --> <!-- 设置类的别名 --> <typeAliases> <!-- 根据包取别名,把包下面的所有类都按类名来取别名 --> <typeAlias type="com.ljh.mybatis.pojo.Student" alias="u"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test1"/> <property name="username" value="root"/> <property name="password" value="1997520"/> </dataSource> </environment> </environments> <!-- pojo的映射文件UserMapper引入到配入到配置文件中 --> <mappers> <mapper class="com.ljh.mybatis.mapper.StudentMapper"/> </mappers> </configuration>
StudentMapper:
package com.ljh.mybatis.mapper;import java.util.ArrayList;import com.ljh.mybatis.pojo.Student;public interface StudentMapper { public ArrayList<Student> getallstudents();}
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="com.ljh.mybatis.mapper.StudentMapper"> <select id="getallstudents" resultType="u"> select * from student where 1=1 </select></mapper>
Student:
package com.ljh.mybatis.pojo;public class Student { private int id; private String stuid; private String password; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStuid() { return stuid; } public void setStuid(String stuid) { this.stuid = stuid; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student(int id, String stuid, String password, String name) { super(); this.id = id; this.stuid = stuid; this.password = password; this.name = name; } public Student() { } }
Test:
package com.ljh.mybatis.test;import java.io.IOException;import java.io.Reader;import java.util.ArrayList;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.ljh.mybatis.mapper.StudentMapper;import com.ljh.mybatis.pojo.Student;public class Test { public static void main(String[] args) { try { //1、加载配置文件 Reader reader=Resources.getResourceAsReader("mybatis-config.xml"); //2、得到sessionfactorybuilder SqlSessionFactoryBuilder sfb=new SqlSessionFactoryBuilder(); //3、得到sqlsessionfactory SqlSessionFactory factory=sfb.build(reader); //4、得到sqlsession SqlSession session=factory.openSession(); //5、得到Mapper映射器 StudentMapper mapper=session.getMapper(StudentMapper.class); ArrayList<Student> stu=mapper.getallstudents(); for(int i=0;i<stu.size();i++){ System.out.println(stu.get(i).getName()); } session.close(); } catch (Exception e) { e.printStackTrace(); } }}
添加回答
举报
0/150
提交
取消