标签:cts name index public insert cat 输入 环境 poj
2. 入门案例
public class Student { private int studentID; private String studentName; private String studentSex; public int getStudentID() { return studentID; } public void setStudentID(int studentID) { this.studentID = studentID; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getStudentSex() { return studentSex; } public void setStudentSex(String studentSex) { this.studentSex = studentSex; } }
<?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/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <!-- 注册studentMapper.xml文件, studentMapper.xml位于com.test.bean这个包下,所以resource写成com/test/bean/studentMapper.xml --> <mapper resource="com/test/bean/studentMapper.xml" /> </mappers> </configuration>
2.把连接数据的信息放在db.properties中,在mybatis-config.xml中引用db.properties; 新建一个db.properties,和mybatis-config.xml在同一目录。内容如下:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=root
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> <!-- 导入db.properties --> <properties resource="db.properties"></properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <!-- 注册studentMapper.xml文件, studentMapper.xml位于com.test.bean这个包下,所以resource写成com/test/bean/studentMapper.xml --> <mapper resource="com/test/bean/studentMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 --> <mapper namespace="com.test.bean.studentMapper"> <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getStudent,id属性值必须是唯一的, 不能够重复 使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型 resultType="com.test.bean.Student" 表示将查询结果封装成一个User类的对象返回 User类就是users表所对应的实体类 --> <!-- 根据id查询得到一个student对象 --> <select id="getStudent" parameterType="int" resultType="com.test.bean.Student"> select * from tb_student where studentID=#{id} </select> <!-- 通过姓名模糊查询,返回结果可能为多个,但是这里的结果类型还是对应一个Java类 --> <select id="findStudentByName" parameterType="java.lang.String" resultType="com.test.bean.Student"> select * from tb_student where studentName like #{value} </select> <!-- 插入一条记录 , 如果tb_student中的主键是自增的,那这里就不用管主键,系统会自动分配--> <insert id="insertStudent" parameterType="com.test.bean.Student"> insert into tb_student (studentName,studentSex) values(#{studentName},#{studentSex}) </insert> </mapper>
public class DBTools { public static SqlSessionFactory sessionFactory; static { try { // 使用MyBatis提供的Resources类加载mybatis的配置文件 Reader reader = Resources.getResourceAsReader("mybatis-config.xml"); // 构建sqlSession的工厂 sessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); } } // 创建能执行映射文件中sql的sqlSession public static SqlSession getSession() { return sessionFactory.openSession(); } }
public class StudentDAO { // 通过ID查询student public void findStudentById(int studentID) { SqlSession session = DBTools.getSession(); /** * 映射sql的标识字符串, * com.test.bean.studentMapper是studentMapper.xml文件中mapper标签的namespace属性的值, * getStudent是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ String statement = "com.test.bean.studentMapper.getStudent";// 映射sql的标识字符串 Student stu1 = session.selectOne(statement, 1); System.out.println(stu1.getStudentID() + "\n" + stu1.getStudentName() + "\n" + stu1.getStudentSex()); session.close(); } // 通过姓名模糊查询 public void findStudentByName(String studentName) { SqlSession session = DBTools.getSession(); String statement = "com.test.bean.studentMapper.findStudentByName";// 映射sql的标识字符串 // 这里不能使用selectOne,因为返回的结构可能是多条 List<Student> list = session.selectList(statement, "%" + studentName + "%"); System.out.println(list.get(0).getStudentName()); session.close(); } // 插入一条学生信息 public void insertStudent(Student student) { SqlSession session = DBTools.getSession(); String statement = "com.test.bean.studentMapper.insertStudent"; session.insert(statement, student); session.commit(); System.out.println("插入成功"); session.close(); } }
标签:cts name index public insert cat 输入 环境 poj
原文地址:http://www.cnblogs.com/hello-daocaoren/p/6793428.html