mybatis学习1--概念,jar包

mybatis.jar包

下载地址
https://github.com/mybatis/mybatis-3/releases
解压文件夹,将lib底下的所有jar包,mybatis-3.2.7.jar包导入到项目
mybatis学习1--概念,jar包_第1张图片
jar包如下
核心包:
mybatis-3.2.7.jar

依赖包如下:
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar

mybatis的初步使用——搭建Mybatis框架:

在ecilipse中新建项目,导入上面的包,再导入数据库jar包。
mysql-connector-java-8.0.16.jar

1. 导入相关jar包;

2. 编写mybatis的相关配置文件;

mybatis.cfg.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>
 <environments default="development">
 <environment id="development">
 <transactionManager type="JDBC"/>
 <dataSource type="POOLED">
 <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost:3306/testmybatis"/>
 <property name="username" value="root"/>
 <property name="password" value="123456"/>
 </dataSource>
 </environment>
 </environments>
 <mappers>
 	<mapper resource="cn/my/entity/user.mapper.xml"/>
 </mappers> 
</configuration>

3. 创建sqlSessionFactory,获取sqlSession;

MybatisUtil .java

public class MybatisUtil {
	//创建SqlSessionFactory
	public static SqlSessionFactory getSqlSessionFactory() throws IOException{
		String recourceString ="mybatis.cfg.xml";
		InputStream inputStream = Resources.getResourceAsStream(recourceString);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		return sqlSessionFactory;		
	}
	//获取SqlSession
	public static SqlSession getSession() throws IOException{
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		return sqlSessionFactory.openSession();
	}

}

4. 创建实体类;

(属性名称应该和数据库一致,否则后续可能找不到。。。)

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;
	}
}

5. 编写sql语句的映射文件;

/spring_mybatis/src/cn/my/entity/user.mapper.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="cn.my.entity.UserMapper">
 <select id="selectUser" resultType="cn.my.entity.User">
 	select * from usermess where id = #{id}
 </select>
</mapper>
  1. 测试
public class tets {

	public static void main(String[] args) throws IOException {
		SqlSession sqlSession = MybatisUtil.getSession();
		User user =sqlSession.selectOne("cn.my.entity.UserMapper.selectUser", 1);
		System.out.println("id "+user.getId()+"   "+user.getName()+"  "+user.getPwd());
		sqlSession.close();
	}

}

出错如下:
mybatis学习1--概念,jar包_第2张图片
原因:(参考链接)

在使用mysql的jdbc驱动最新版(6.0+)版本时,数据库和系统时区差异引起的问题。

解决办法:

  1. 一种是降版本,并不推荐,如果需要降版本5.5版本可以满足基本需要;
  2. 还有一种是在jdbc连接的url后面加上serverTimezone=UTC或GMT即可,如果需要指定使用gmt+8时区,需要写成GMT%2B8,不然可能会报错误,解析为空

示例如下:

jdbc.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&characterEncoding=utf-8

我用第二个方法显示的是某些语法错误(不想找原因了》-《 );找了个较低版本的数据库驱动包。

curd实现内容——增删查改基本操作:

mabitis.cfg.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>
 <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/testmybatis"/>
 <property name="username" value="root"/>
 <property name="password" value="123456"/>
 </dataSource>
 </environment>
 </environments>
 <mappers>
 	<mapper resource="cn/my/entity/user.mapper.xml"/>
 </mappers> 
</configuration>

user.mapper.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="cn.my.entity.UserMapper">
 <select id="selectUser" resultType="cn.my.entity.User">
 	select * from usermess where id = #{id}
 </select>
 <!-- parameterType参数类型,resultType结果类型 -->
 <insert id="addUser" parameterType="cn.my.entity.User" useGeneratedKeys="true">
 	insert into usermess(name,password) values(#{name},#{password})
 </insert>
 <update id="updateUser" parameterType="cn.my.entity.User">
 		update usermess set name=#{name},password=#{password}where id=#{id}
 </update>
 <delete id="deleteUser">
 	delete from usermess where id = #{id}
 </delete>
 
 <select id="selectAll" resultType="cn.my.entity.User">
 	Select * from usermess
 </select>
</mapper>

UserDao .java

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.my.entity.User;
import cn.my.util.MybatisUtil;

public class UserDao {
	
	public List<User> allUser() throws IOException{
		SqlSession sqlSession = MybatisUtil.getSession();
		List<User> user = new ArrayList<User>();
		user =sqlSession.selectList("cn.my.entity.UserMapper.selectAll");
		sqlSession.close();
		return user;
	}
		public User getById(int id) throws IOException{
			SqlSession sqlSession = MybatisUtil.getSession();
			User user =sqlSession.selectOne("cn.my.entity.UserMapper.selectUser", id);
			sqlSession.close();
			return user;
		}
		public int addUser(User user) throws IOException{
			SqlSession sqlSession = MybatisUtil.getSession();
			int result = sqlSession.insert("cn.my.entity.UserMapper.addUser",user);
			sqlSession.commit();//提交事务,在jdbc中默认提交
			sqlSession.close();
			return result;
		}
		public int updateUser(User user) throws IOException{
			SqlSession sqlSession = MybatisUtil.getSession();
			int result = sqlSession.update("cn.my.entity.UserMapper.updateUser",user);
			sqlSession.commit();//提交事务,在jdbc中默认提交
			sqlSession.close();
			return result;
		}
		
		public int deleteUser(int id) throws IOException{
			SqlSession sqlSession = MybatisUtil.getSession();
			int result = sqlSession.delete("cn.my.entity.UserMapper.deleteUser",id);
			sqlSession.commit();//提交事务,在jdbc中默认提交
			sqlSession.close();
			return result;
		}
		
}

你可能感兴趣的:(#,mybatis)