/*数据库编码UTF8,以下命令是为了在脚本和
命令行中支持中文*/
set names gbk;
/*创建courseman数据库*/
dropdatabase if exists courseman;
createdatabase courseman;
/*切换到courseman数据库*/
use courseman;
/*创建学生表*/
CREATETABLE student(
id intNOTNULL AUTO_INCREMENT primarykey,
namevarchar(10) NOTNULL,/*姓名*/
gender char(1) NOTNULL,/*性别*/
major varchar(20) NOTNULL,/*专业*/
grade char(4) NOTNULL/*年级*/
);
/*授予courseman用户访问courseman数据库的全部权限。
为方便起见,用户名与数据库同名。该用户若不存在则被
创建,密码为abc123*/
grantallprivilegeson courseman.* to courseman@'%'
identified by'abc123';
flush privileges;
/*添加第一条记录*/
insertinto student(name, gender, major, grade)
values('李林','男','计算机科学与技术','2011');
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--表明重用预编译的SQL语句-->
<settings>
<settingname="defaultExecutorType"value="REUSE"/>
</settings>
<!--类型别名定义。今后可只用Student来代替它冗长的
全限定名-->
<typeAliases>
<typeAliasalias="Student"type="com.abc.domain.Student"/>
</typeAliases>
<!--environments可包含多个environment元素。
每个environment配置与数据库交互的细节,这里
只需要配置一个。default属性是指在创建SqlSessionFactory
时,若没有明确指定要用哪个environment,则使用此
属性指定的-->
<environmentsdefault="development">
<environmentid="development">
<transactionManagertype="jdbc"/>
<!--使用连接池的数据源配置-->
<dataSourcetype="POOLED">
<propertyname="driver"value="com.mysql.jdbc.Driver"/>
<propertyname="url"
value="jdbc:mysql://localhost/courseman"/>
<propertyname="username"value="courseman"/>
<propertyname="password"value="abc123"/>
</dataSource>
</environment>
</environments>
<!--指定要用到的mapper文件。以下的resource属性告诉
MyBatis要在类路径下的resources目录下找StudentMapper.xml文件。我们将把mapper文件存放在src目录下的resources目录中,并让ant把resources目录拷贝到classes中,而classes目录会被ant添加到类路径中。-->
<mappers>
<mapperresource="resources/StudentMapper.xml"/>
</mappers>
</configuration>
package com.abc.domain;
publicclass Student {
privateint id;
private String name; //姓名
private String gender; //性别
private String major; //专业
private String grade; //年级
publicint getId() {
return id;
}
publicvoid setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
publicvoid setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
publicvoid setGender(String gender) {
this.gender = gender;
}
public String getMajor() {
return major;
}
publicvoid setMajor(String major) {
this.major = major;
}
public String getGrade() {
return grade;
}
publicvoid setGrade(String grade) {
this.grade = grade;
}
}
package com.abc.mapper;
import com.abc.domain.Student;
publicinterface StudentMapper {
//根据学生ID查询学生实体
public Student getById(int id);
}
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace该是StudentMapper的完整限定名-->
<mappernamespace="com.abc.mapper.StudentMapper">
<!--定义java bean的属性与数据库表的列之间的映射。type="Student"用到了configuration.xml中定义的别名-->
<resultMapid="studentResultMap"type="Student">
<!--id映射-->
<idproperty="id"column="id"/>
<!--普通属性映射-->
<resultproperty="name"column="name"/>
<resultproperty="gender"column="gender"/>
<resultproperty="major"column="major"/>
<resultproperty="grade"column="grade"/>
</resultMap>
<!--与StudentMapper接口中的getById方法对应,包括
方法名和参数类型。SQL语句中以“#{}”的形式引用参数-->
<selectid="getById"parameterType="int"resultMap="studentResultMap">
SELECT *
FROM student WHERE id = #{id}
</select>
</mapper>
package com.test;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.abc.mapper.StudentMapper;
import com.abc.domain.Student;
publicclass TestMyBatis
{
publicstaticvoid main(String[] args)
{
//与configuration.xml中的mapper配置类似,告诉MyBatis
//应读取的核心配置文件
String resource = "resources/configuration.xml";
Reader reader = null;
try{
reader = Resources.getResourceAsReader(resource);
}catch(IOException e)
{
e.printStackTrace();
}
//创建SqlSessionFactory实例。没有指定要用到的
//environment,则使用默认的environment
SqlSessionFactory sqlSessionFactory
= new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
try{
StudentMapper mapper =
sqlSession.getMapper(StudentMapper.class);
Student student = mapper.getById(1);
if(student != null)
{
System.out.println("姓名: "+student.getName()
+"\n专业: "+student.getMajor());
}
else
{
System.out.println("没有找到。");
}
}
finally
{
sqlSession.close();
}
}
}