1、导入相关的jar包:mysql.jar、mybatis3.jar
2、src下,编写mysql.properties文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/cs_db
username=root
password=123
3、src下编写mybatis-config.xml总配置文件
XML申明:
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">ML样例:
<?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>
<!-- 驱动配置文件 -->
<properties resource="mysql.properties"/>
<!-- 别名映射 -->
<typeAliases>
<!-- 自动扫描包下的Bean -->
<package name="me.mxzf.domain"/>
<typeAlias type="me.mxzf.domain.Student" alias="Student"/>
</typeAliases>
<!-- 环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- Bean映射Sql文件 -->
<mappers>
<mapper resource="me/mxzf/mapping/StudentMapper.xml" />
</mappers>
</configuration>
4、编写对应的类的sql映射文件
XML申明:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
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="me.mxzf.mapping.StudentMapper">
<select id="selectStudent" resultType="Student">
select * from student where sid = #{sid}
</select>
</mapper>
6、通过namespace+id的方式去访问对应的sql语句
//从XML中构建SqlSessionFactory
//如果配置文件在src目录下,resource = "mybatis-config.xml";
//否则resource = 具体的包路径 + 文件名
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//在总配置文件中mappers里注册所有的sql映射文件
<mapper resource="me/mxzf/mapping/StudentMapper.xml"/>
...
//从SqlSessionFactory中获取SqlSession
SqlSession session = sessionFactory.openSession();
try {
//me.mxzf.mapping.StudentMapper 命名空间
//insertStudent 在此命名空间下有个id为insertStudent的mapper
session.insert("me.mxzf.mapping.StudentMapper.insertStudent",
student);
session.commit();
} catch (Exception e) {
session.rollback();
} finally {
session.close();
}
在总配置文件中配置Bean别名(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,plugins?,environments?,databaseIdProvider?,mappers?)
<typeAliases>
<!-- 自动扫描包下的Bean -->
<package name="me.mxzf.domain"/>
<typeAlias type="me.mxzf.domain.Student" alias="Student"/>
</typeAliases>
7、通过接口和java注解的方式
//接口
public interface StudentDAO {
@Select("select * from student where sid=#{sid}")
public Student selectStudentById(int sid);
}
Mybatis注解学习 – xxxMapper is not known to the MapperRegistry
Mybatis注解一定要注册自己写的接口类,不然就会老报开头提到的这个错误
1、factory.getConfiguration().addMapper(UserInfoMapper.class);//动态注册
2、 //静态注册
3、演示CRUD操作
1、UserMapper接口文件
public interface UserMapper {
@Insert("insert into users(name, age) values(#{name}, #{age})")
public int add(Users user);
@Delete("delete from users where id = #{id}")
public int deleteById(int id);
@Update("update users set name = #{name}, age = #{age} where id = #{id}")
public int update(Users user);
@Select("select * from users where id = #{id}")
public Users getUserById(int id);
@Select("select * from users")
public List<Users> getAllUsers();
}
2、在总配置文件mappers中注册UserMapper这个接口
<mapper class="com.bird.mybatis.bean.UserMapper"/>
3、使用这个类
SqlSession openSession = factory.openSession();
UserMapper mapper = openSession.getMapper(UserMapper.class);
//动态注入到接口中
mapper.add(new Users(-1,"娃娃",99));
//提交修改
openSession.commit();
openSession.close();