今天同学说要学习mybatis后来他写了个程序让我看看,我看了一下发现包引错了,他写的是单独的mybatis,引入的却是spring-mybatis,所以会报错。
今天我记录一下单独mybatis的使用
(1)首先找jar包,你也可以使用maven,我是引入的jar包
需要两个包1.mybatis 2驱动包,这个包根据自己使用的数据库去找。
(2)开始写代码
两个配置文件
UserMapper.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="com.xxx.demo.mapper.UserMapper">
<!--根据id查询数据-->
<select id="findUserById" parameterType="int" resultType="User">
select * from t_user where id = #{id}
</select>
<!--查询所有数据-->
<select id="findAllUser" resultType="User">
SELECT * from t_user
</select>
<!--插入数据-->
<insert id="addUser" parameterType="User" keyProperty="id" useGeneratedKeys="true">
INSERT INTO t_user(username,password) values(#{username},#{password})
</insert>
<!--删除数据-->
<delete id="deleteUserById" parameterType="int">
DELETE from t_user WHERE id= #{id}
</delete>
<update id="updateUser" parameterType="User" useGeneratedKeys="true" keyProperty="id" >
UPDATE t_user SET username=#{username},password=#{password}
WHERE id=#{id}
</update>
</mapper>
mybatis-config.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>
<typeAliases>
<typeAlias alias="User" type="com.xxx.demo.pojo.User" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1:5432/Solution" />
<property name="username" value="postgres" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- // power by http://www.yiibai.com -->
<mapper resource="com/xxx/demo/mapper/UserMapper.xml" />
</mappers>
</configuration>
User.java
package com.xxx.demo.pojo;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserMapper.java
package com.xxx.demo.mapper;
import com.xxx.demo.pojo.User;
import java.util.List;
/**
* Mapper接口
*/
public interface UserMapper {
//根据id查询数据
User findUserById(int id);
//查询所有数据
List<User> findAllUser();
//添加User
int addUser(User u);
//根据id删除数据
int deleteUserById(int id);
//修改User
int updateUser(User user);
}
GetSessionFactory.java
package com.xxx.demo.Test;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
/**
* 创建sqlSession
*/
public class GetSessionFactory {
private static SqlSessionFactory sqlSessionFactory;
private GetSessionFactory(){
}
synchronized public static SqlSessionFactory getSqlSessionFactory(){
if(sqlSessionFactory==null){
String resources="com/xxx/demo/resources/mybatis-config.xml";
InputStream inputStream=null;
try {
inputStream= Resources.getResourceAsStream(resources);
}catch (Exception e){
e.printStackTrace();
}
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}
return sqlSessionFactory;
}
}
Test.java
package com.xxx.demo.Test;
import com.xxx.demo.mapper.UserMapper;
import com.xxx.demo.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import java.util.List;
public class TestDemo {
public static void main(String[] args) {
SqlSessionFactory sqlSessionFactory=GetSessionFactory.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User u=mapper.findUserById(1);
System.out.println(u);
}
/**
* 查找所有
*/
@Test
public void fun(){
SqlSessionFactory sqlSessionFactory=GetSessionFactory.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> allUser = mapper.findAllUser();
for (User u:allUser) {
System.out.println(u);
}
}
/**
* 添加
*/
@Test
public void fun2(){
User user=new User();
user.setUsername("ywl");
user.setPassword("ywl");
System.out.println(user);
SqlSessionFactory sqlSessionFactory=GetSessionFactory.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.addUser(user);
sqlSession.commit();
System.out.println(user);
System.out.println(i);
}
/**
* 删除
*/
@Test
public void fun3(){
SqlSessionFactory sqlSessionFactory=GetSessionFactory.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.deleteUserById(1);
sqlSession.commit();
System.out.println(i);
}
@Test
public void fun4(){
User u=new User();
u.setId(7);
u.setPassword("hsj18731362155");
u.setUsername("胡少君");
SqlSessionFactory sqlSessionFactory=GetSessionFactory.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.updateUser(u);
sqlSession.commit();
System.out.println(i);
}
}
注意问题
(1)数据库自增问题。
(2)保存的时候事物提交问题。
(3)导包问题,是单独的mybatis包,不是mybatis-spring包
(4)有什么问题咨询我啊,qq 1058179540