Mybatis 学习记录 续
项目结构如下:
1、数据库建表
表名:user
结构:
内容:
2、pom.xml文件更新如下:
注:其中build部分尤其需要重视
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>Mybatis_test</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>mybatis01</module> </modules> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies>
<!--在build中配置resources,来防止我们资源导出失败的问题--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
3、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"> <!--命名空间,绑定一个Dao--> <mapper namespace="com.demo.dao.Userdao"> <!--查询语句--> <select id="getUserList" resultType="com.demo.pojo.User"> select * from calss.user </select> </mapper>
4、测试类
直接上代码了
package com.demo.dao; import com.demo.pojo.User; import com.demo.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; /** * @author June * @date 2022/1/7 14:16 */ public class UserDaoTest { @Test public void test() { //第一步:获取sqlSession对象 SqlSession sqlSession= MybatisUtils.getSqlSession(); try { //getMapper 方式一 Userdao userdao=sqlSession.getMapper(Userdao.class); List<User> userList=userdao.getUserList(); //方式二 //List<User> userList1=sqlSession.selectList("com.demo.dao.Userdao.getUserList"); for (User user:userList) { System.out.println(user.getId()+user.getName()+user.getPwd()); } }catch (Exception e){ e.printStackTrace(); }finally { //关闭 sqlSession.close(); } } }
测试结果如下:
终版,完善后(即添加了CRUD后)的相关代码如下:
Userdao.java
package com.demo.dao; import com.demo.pojo.User; import java.util.List; /** * @author June * @date 2022/1/6 17:44 */ public interface Userdao { //获取全部用户 List<User> getUserList(); //根据id查询用户 User getUserbyid(int id); //insert 插入一个用户 int addUser(User user); //update用户 int updateUser(User user); //删除一个用户 int deleteUser(int id); }
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"> <!--命名空间,绑定一个Dao--> <mapper namespace="com.demo.dao.Userdao"> <!--查询语句--> <select id="getUserList" resultType="com.demo.pojo.User"> select * from calss.user </select> <select id="getUserbyid" parameterType="int" resultType="com.demo.pojo.User"> select * from calss.user where id = #{id} </select> <insert id="addUser" parameterType="com.demo.pojo.User"> insert into calss.user (id,name,pwd) values (#{id},#{name},#{pwd}); </insert> <update id="updateUser" parameterType="com.demo.pojo.User"> update calss.user set name = #{name},pwd = #{pwd} where id = #{id}; </update> <delete id="deleteUser" parameterType="int"> delete from calss.user where id = #{id}; </delete> </mapper>
UserDaoTest.java
package com.demo.dao; import com.demo.pojo.User; import com.demo.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; /** * @author June * @date 2022/1/7 14:16 */ public class UserDaoTest { //查询所有用户 @Test public void test() { //第一步:获取sqlSession对象 SqlSession sqlSession= MybatisUtils.getSqlSession(); try { //getMapper 方式一 Userdao userdao=sqlSession.getMapper(Userdao.class); List<User> userList=userdao.getUserList(); //方式二 //List<User> userList1=sqlSession.selectList("com.demo.dao.Userdao.getUserList"); for (User user:userList) { System.out.println(user.getId()+user.getName()+user.getPwd()); } }catch (Exception e){ e.printStackTrace(); }finally { //关闭 sqlSession.close(); } } //根据id查询用户 @Test public void getUserbyid() { //第一步:获取sqlSession对象 SqlSession sqlSession= MybatisUtils.getSqlSession(); try { //getMapper 方式一 Userdao userdao=sqlSession.getMapper(Userdao.class); User user=userdao.getUserbyid(1); //方式二 //List<User> userList1=sqlSession.selectList("com.demo.dao.Userdao.getUserList"); System.out.println(user.getId()+user.getName()+user.getPwd()); }catch (Exception e){ e.printStackTrace(); }finally { //关闭 sqlSession.close(); } } //添加一个用户 @Test public void addUser() { //第一步:获取sqlSession对象 SqlSession sqlSession= MybatisUtils.getSqlSession(); try { //getMapper 方式一 Userdao userdao=sqlSession.getMapper(Userdao.class); int number = userdao.addUser(new User(3,"信","333")); if(number!=0){ System.out.println("插入成功!!!"); } //提交事务 sqlSession.commit(); //方式二 //List<User> userList1=sqlSession.selectList("com.demo.dao.Userdao.getUserList"); }catch (Exception e){ e.printStackTrace(); }finally { //关闭 sqlSession.close(); } } //修改某个用户 @Test public void updateUser() { //第一步:获取sqlSession对象 SqlSession sqlSession= MybatisUtils.getSqlSession(); try { //getMapper 方式一 Userdao userdao=sqlSession.getMapper(Userdao.class); int number = userdao.updateUser(new User(3,"sndi","323243")); if(number!=0){ System.out.println("修改成功!!!"); } //提交事务 sqlSession.commit(); //方式二 //List<User> userList1=sqlSession.selectList("com.demo.dao.Userdao.getUserList"); }catch (Exception e){ e.printStackTrace(); }finally { //关闭 sqlSession.close(); } } //删除某个用户 @Test public void deleteUser() { //第一步:获取sqlSession对象 SqlSession sqlSession= MybatisUtils.getSqlSession(); try { //getMapper 方式一 Userdao userdao=sqlSession.getMapper(Userdao.class); int number = userdao.deleteUser(3); if(number!=0){ System.out.println("删除成功!!!"); } //提交事务 sqlSession.commit(); //方式二 //List<User> userList1=sqlSession.selectList("com.demo.dao.Userdao.getUserList"); }catch (Exception e){ e.printStackTrace(); }finally { //关闭 sqlSession.close(); } } }