mybatis基础学习二
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis基础学习二相关的知识,希望对你有一定的参考价值。
操作users表的CRUD
XML实现方式
- 定义sql映射xml文件 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="mybatis.userMapper">
<!-- <select id="getid" parameterType="int"
resultType="mybatis.User">
select * from users where id=#{id}
</select> -->
<insert id="insertUser" parameterType="mybatis.User">
insert into users(name, age) values(#{name}, #{age});
</insert>
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
<update id="updateUser" parameterType="mybatis.User">
update users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="selectUser" parameterType="int" resultType="mybatis.User">
select * from users where id=#{id}
</select>
<select id="selectAllUsers" resultType="mybatis.User">
select * from users
</select>
</mapper>
- 在conf.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://172.171.2.161:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root123" />
</dataSource>
</environment>
</environments>
<mappers>
<!--xml方式 -->
<mapper resource="mybatis/userMapper.xml"/>
</mappers>
</configuration>
- 封装得到session的方法,MybatisUtil.java
package mybatis;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtil
{
//返回session
public static SqlSession openSqlSession()
{
String resource = "conf.xml";
InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
//貌似可有可无
session.commit();
return session;
}
}
- 编写测试类 Test.java
插入用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* xml方式测试
*/
String statement = "mybatis.userMapper.insertUser";
int tuser = MybatisUtil.openSqlSession().insert(statement, new User(3, "rose", 3));
MybatisUtil.openSqlSession().close();
System.out.println(tuser);
}
}
更新用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* xml方式测试
*/
String statement = "mybatis.userMapper.updateUser";
int tuser = MybatisUtil.openSqlSession().update(statement, new User(3, "rose", 3));
MybatisUtil.openSqlSession().close();
System.out.println(tuser);
}
}
删除用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* xml方式测试
*/
String statement = "mybatis.userMapper.deleteUser";
int tuser = MybatisUtil.openSqlSession().delete(statement,2));
MybatisUtil.openSqlSession().close();
System.out.println(tuser);
}
}
查询全部用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* xml方式测试
*/
String statement = "mybatis.userMapper.selectAllUsers";
List<User> tuser = MybatisUtil.openSqlSession().selectList(statement);
MybatisUtil.openSqlSession().close();
System.out.println(tuser);
}
}
注解实现方式
- 定义sql接口文件 UserMapperDe.java
package mybatis;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
public interface UserMapperDe
{
@Insert("insert into users(name, age) values(#{name}, #{age})")
public int insertUser(User user);
@Delete("delete from users where id=#{id}")
public int deleteUserById(int id);
@Update("update users set name=#{name},age=#{age} where id=#{id}")
public int updateUser(User user);
@Select("select * from users where id=#{id}")
public User getUserById(int id);
@Select("select * from users")
public List<User> getAllUser();
}
- 在conf.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://172.171.2.161:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root123" />
</dataSource>
</environment>
</environments>
<mappers>
<!--xml方式 -->
<mapper resource="mybatis/userMapper.xml"/>
<!--注解方式 -->
<mapper class="mybatis.UserMapperDe"/>
</mappers>
</configuration>
- 编写测试类 TestClass.java
插入用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* 注解方式测试
*/
UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
int inuser = mapper.insertUser(new User(-1, "clock", 1));
MybatisUtil.openSqlSession().close();
System.out.println(inuser);
}
}
更新用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* 注解方式测试
*/
UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
int inuser = mapper.updateUser(new User(2, "efpd", 2));
MybatisUtil.openSqlSession().close();
System.out.println(inuser);
}
}
删除用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* 注解方式测试
*/
UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
int inuser = mapper.deleteUserById(2);
MybatisUtil.openSqlSession().close();
System.out.println(inuser);
}
}
查询全部用户
package mybatis;
import java.io.IOException;
public class Test
{
public static void main(final String[] args) throws IOException
{
/**
* 注解方式测试
*/
UserMapperDe mapper = MybatisUtil.openSqlSession().getMapper(UserMapperDe.class);
List<User> inuser = mapper.getAllUser();
MybatisUtil.openSqlSession().close();
System.out.println(inuser);
}
}
以上是关于mybatis基础学习二的主要内容,如果未能解决你的问题,请参考以下文章
MyBatis学习总结——使用MyBatis对表执行CRUD操作