标签:maven mybatis 入参 div 最简 最好 它的 use ...
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
CREATE TABLE tb_teacher
(
id bigint NOT NULL auto_increment COMMENT ‘ID‘,
no varchar(10) NOT NULL COMMENT ‘教师编号‘,
name varchar(50) NOT NULL COMMENT ‘教师姓名‘,
sex char(1) NOT NULL COMMENT ‘教师性别‘,
job_title varchar(50) NOT NULL COMMENT ‘职称‘,
PRIMARY KEY (id)
) COMMENT = ‘教师信息表‘;
INSERT INTO tb_teacher (no, name, sex, job_title) VALUES (‘001‘, ‘Test01‘, ‘f‘, ‘教师01‘);
INSERT INTO tb_teacher (no, name, sex, job_title) VALUES (‘002‘, ‘Test02‘, ‘f‘, ‘教师02‘);
INSERT INTO tb_teacher (no, name, sex, job_title) VALUES (‘003‘, ‘Test03‘, ‘f‘, ‘教师03‘);
package model;
import java.io.Serializable;
/** 教师信息表 */
public class Teacher implements Serializable
{
private static final long serialVersionUID = 1L;
/** ID */
private Long id;
/** 教师编号 */
private String no;
/** 教师姓名 */
private String name;
/** 教师性别 */
private String sex;
/** 职称 */
private String job_title;
......
}
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
<?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="lizhiwei" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapping/TeacherMapper.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="mapping.TeacherDao">
<select id="getTeacherById" resultType="model.Teacher" parameterType="_int">
select * from tb_teacher WHERE id = #{id};
</select>
</mapper>
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
Teacher teacher = sqlSession.selectOne("mapping.TeacherDao.getTeacherById",1);
System.out.println(teacher);
sqlSession.close();
package mapping;
import model.Teacher;
public interface TeacherDao
{
public Teacher getTeacherById(int id);
}
TeacherDao teacherDao = sqlSession.getMapper(TeacherDao.class);
teacher = teacherDao.getTeacherById(2);
System.out.println(teacher);
sqlSession.close();
标签:maven mybatis 入参 div 最简 最好 它的 use ...
原文地址:http://www.cnblogs.com/wchxj/p/6438164.html