标签:throws value this select sel nec selectall jdbc nfa
create database mybatis; use mybatis; CREATE TABLE users( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(20), age INT ); INSERT INTO users(NAME, age) VALUES(‘Tom‘, 12); INSERT INTO users(NAME, age) VALUES(‘Jack‘, 11);
<?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://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> </configuration>
public class Users { private int id; private String name; private int age; //get,set 方法 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Users(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public Users() { } public Users(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Users [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
<?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.zhiyou100.hhz.mapper.UsersMapper"> <select id="getUser" parameterType="int" resultType="com.zhiyou100.hhz.bean.Users"> select * from users where id=#{id} </select> <insert id="addUser" parameterType="com.zhiyou100.hhz.bean.Users"> insert into users(name,age) value(#{name},#{age}) </insert> <update id="updateUser" parameterType="com.zhiyou100.hhz.bean.Users"> update Users set name=#{name},age=#{age} where id=#{id} </update> <delete id="deleteUser" parameterType="int"> delete from Users where id=#{id} </delete> <select id="getUserAll" resultType="com.zhiyou100.hhz.bean.Users"> select * from users </select> <select id="selectUserByAge" parameterType="java.util.Map" resultType="com.zhiyou100.hhz.bean.Users"> <![CDATA[select * from users where age>=10 and age<=30]]> </select> </mapper>
<mappers> <mapper resource="com/zhiyou100/hhz/mapper/UsersMapper.xml" /> </mappers>
class UsersTest { final String str="com.zhiyou100.hhz.mapper.UsersMapper."; static SqlSession session = null; @BeforeAll static void setUpBeforeClass() throws Exception { Reader reader = Resources.getResourceAsReader("conf.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); session = sessionFactory.openSession(); } @Test void test1() { Users users = session.selectOne(str+"getUser",1); System.out.println(users); } @Test void test2() { session.insert(str+"addUser",new Users("朱华龙",1)); } @Test void test3() { session.update(str+"updateUser",new Users(1,"王玉芳",25)); } @Test void test4() { session.delete(str+"deleteUser",6); } @Test void testselectAll() { List<Users> list = session.selectList(str+"getUserAll"); for(Users users:list) { System.out.println(users); } } @Test void testselectByAge() { Map<String, Integer> map=new HashMap<String,Integer>(); map.put("min", 10); map.put("max", 30); List<Users> list = session.selectList(str+"selectUserByAge",map); for(Users users:list) { System.out.println(users); } } @AfterAll static void tearDownAfterClass() throws Exception { session.commit(); }
标签:throws value this select sel nec selectall jdbc nfa
原文地址:https://www.cnblogs.com/zfyyfw/p/11438301.html