下载地址
https://github.com/mybatis/mybatis-3/releases
解压文件夹,将lib底下的所有jar包,mybatis-3.2.7.jar包导入到项目
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
在ecilipse中新建项目,导入上面的包,再导入数据库jar包。
mysql-connector-java-8.0.16.jar
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>
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();
}
}
(属性名称应该和数据库一致,否则后续可能找不到。。。)
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;
}
}
/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>
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();
}
}
在使用mysql的jdbc驱动最新版(6.0+)版本时,数据库和系统时区差异引起的问题。
解决办法:
示例如下:
jdbc.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&characterEncoding=utf-8
我用第二个方法显示的是某些语法错误(不想找原因了》-《 );找了个较低版本的数据库驱动包。
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;
}
}