1、相关jar包
只需要mybatis\mybatis-generator-core\mysql-connection
Junit \log4j 系列都是辅助使用,可有可无
Mybatis-spring.jar 是spring是整合mybatis所需要的jar
2、配置
- <?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>
- <!-- 取别名 -->
- <typeAliases>
- <package name="cn.tri.sm.pojo"/>
- <package name="cn.tri.sm.dao"/>
- <package name="cn.tri.sm.daoImpl"/>
- </typeAliases>
- <!--配置数据库信息-->
- <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/project"/>
- <property name="username" value="root"/>
- <property name="password" value="admin"/>
- </dataSource>
- </environment>
- </environments>
- <!-- 扫描映射文件路径 -->
- <mappers>
- <mapper resource="cn\tri\sm\mapper\UserDao.xml"/>
- </mappers>
- </configuration>
|
userMapper.xml文件中
Namespace="命名空间" 可以随便取,但是最好独一无二,与XXDao类似。
- <?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="UserDao">
- <select
id="getId"
parameterType="int"
resultType="User">
- select * from user where id=#{id}
- </select>
- </mapper>
|
3.demo
目录结构
//user、必须要有无参的构造方法
public class User {
- private
int id ;
- private String name ;
- private String pwd ;
- public
int
getId() {
- return id;
- }
- public
void
setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public
void
setName(String name) {
- this.name = name;
- }
- public String getPwd() {
- return pwd;
- }
- public
void
setPwd(String pwd) {
- this.pwd = pwd;
- }
- public
User(int id, String name, String pwd) {
- super();
- this.id = id;
- this.name = name;
- this.pwd = pwd;
- }
- public
User() {
- super();
- }
- @Override
- public String toString() {
- return
"User [id="
+ id +
", name="
+ name +
", pwd="
+ pwd +
"]";
- }
- }
|
//userDao
- public interface UserDao {
- public User getId(int id);
- }
- //userDaoImpl、此处必须拿到sqlsessionFactory
public class UserDaoImpl implements UserDao{
- private SqlSessionFactory factory;
- // 带 sqlsessionFactory参数的构造函数
public
UserDaoImpl(SqlSessionFactory factory){
- this.factory = factory;
- }
- public User getId(int id) {
- SqlSession sqlSession = factory.openSession();
- User user = sqlSession.selectOne("UserDao.getId",id);
- sqlSession.close();
- return user;
- }
- }
|
//测试
- public static
void
main(String[] args) {
- String resource =
"mybaties-config.xml";
- InputStream is = null;
- try
{
- is = Resources.getResourceAsStream(resource);
- }
catch
(IOException e) {
- e.printStackTrace();
- }
- SqlSessionFactory factory =
new
SqlSessionFactoryBuilder().build(is);
- UserDaoImpl userDao =
new
UserDaoImpl(factory);
- User user = userDao.getId(8);
- System.out.println(user);
-
- }
|
- <!--idea的maven项目中,java程序不会读取非resources里的xml文件,需加入提示他要读取在指定路径的xml-->
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- </resource>
- </resources>
|