在第一个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 + '}'; } }
增加一个查询接口:
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(); void saveUser(User user); }
修改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> </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(); User user = new User(); user.setId(1001); user.setName("s-s-x-1002"); user.setAge(1002); UserDao userdao = ss.getMapper(UserDao.class); userdao.saveUser(user); ss.commit(); //4. 释放资源 ss.close(); config.close(); } }
执行结果:
============================================================================================================
修改上面返回类型为int,其它不变:
标签:java,name,age,CRUD,id,Mybatis,import,config,public From: https://www.cnblogs.com/xiaobaibailongma/p/16929886.html