在第一个xml示例项目的基础上,进行操作:
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> <!-- 配置mybatis运行环境 --> <environments default="development"> <environment id="development"> <!-- 使用JDBC的事务管理 --> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <!-- MySQL数据库驱动 --> <property name="driver" value="com.mysql.jdbc.Driver" /> <!-- 连接数据库的URL --> <property name="url" value="jdbc:mysql://localhost:3306/mysql8?characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <!-- 将mapper文件加入到配置文件中 --> <mappers> <mapper resource="com/sunxl/dao/UserDao.xml"/> </mappers> </configuration>
pojo类保持不变:
package com.sunxl.pojo; public class User { Integer id; String name; Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Users{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
增加DAO接口:
package com.sunxl.dao; import com.sunxl.pojo.User; import org.apache.ibatis.annotations.Select; import java.util.List; public interface UserDao { List<User> findAll(); int saveUser(User user); int updateUser(User user); int deleteUser(Integer id); User findById(Integer id); List<User> findByName(String name); }
修改mapper映射文件:
<?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.sunxl.dao.UserDao"> <!-- 查询所有网站信息 --> <select id="findAll" resultType="com.sunxl.pojo.User"> select * from USERs </select> <insert id="saveUser" parameterType="com.sunxl.pojo.User"> insert into users(id,name,age) values(#{id},#{name},#{age}) </insert> <update id="updateUser" parameterType="com.sunxl.pojo.User"> update users set name = #{name},age = #{age} where id = #{id} </update> <delete id="deleteUser" parameterType="java.lang.Integer"> delete from users where id = #{id} </delete> <select id="findById" parameterType="java.lang.Integer" resultType="com.sunxl.pojo.User"> select * from USERs where id = #{id} </select> <select id="findByName" parameterType="java.lang.String" resultType="com.sunxl.pojo.User"> select * from USERs where name like #{name}; </select> </mapper>
执行操作:
import com.sunxl.dao.UserDao; import com.sunxl.pojo.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.util.List; public class MybatisTest { public static void main(String[] args) throws IOException { // InputStream config = Resources.getResourceAsStream("mybatis-config.xml"); // // SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); // // SqlSessionFactory factory = builder.build(config); // // SqlSession ss = factory.openSession(); // // UserDao userdao = ss.getMapper(UserDao.class); // // List<User> users = userdao.findAll(); // // for (User user: users){ // System.out.println(user); // } // // ss.commit(); // // //4. 释放资源 // ss.close(); // // config.close(); InputStream config = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(config); SqlSession ss = factory.openSession(); UserDao userdao = ss.getMapper(UserDao.class); List<User> users = userdao.findByName("%abc%"); for (User user: users){ System.out.println(user); } ss.commit(); //4. 释放资源 ss.close(); config.close(); } }
结果核实:
标签:java,name,age,CRUD,public,Mybatis,import,config,id From: https://www.cnblogs.com/xiaobaibailongma/p/16930083.html