1. 代码 51
com.powernode.godbatis.pojo
User
package com.powernode.godbatis.pojo;
//这是一个pojo用来测试我们的最终版godbatis的 51
public class User {
private String id;
private String name;
private String age;
public User(String id, String name, String age) {
this.id = id;
this.name = name;
this.age = age;
}
public User() {
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
com.powernode.godbatis.test
UsrMapperTest
package com.powernode.godbatis.test;
import com.powernode.godbatis.pojo.User;
import org.god.ibatis.core.SqlSession;
import org.god.ibatis.core.SqlSessionFactory;
import org.god.ibatis.core.SqlSessionFactoryBuilder;
import org.god.ibatis.utils.Resources;
import org.junit.Test;
//测试我们的最终版godbatis的 51
public class UsrMapperTest {
@Test
public void testInsertUser(){
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("godbatis-config.xml"));
SqlSession sqlSession = build.openSession();
//执行sql语句
User user = new User("3333", "孙悟空", "5000");
int count = sqlSession.insert("user.insertUser", user);
System.out.println(count);
sqlSession.commit();
sqlSession.close();
}
@Test
public void testSelectById(){
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("godbatis-config.xml"));
SqlSession sqlSession = sqlSessionFactory.openSession();
Object obj = sqlSession.selectOne("user.selectById", "3333");
System.out.println(obj);
sqlSession.close();
}
}
pom.xml
<?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.powernode</groupId>
<artifactId>course9</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 依赖-->
<dependencies>
<!-- godbatis依赖-->
<dependency>
<groupId>org.god.ibatis</groupId>
<artifactId>course7</artifactId>
<version>1.0</version>
</dependency>
<!-- 引入junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
godbatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/powernode"/>
<property name="username" value="root"/>
<property name="password" value="lzl"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<mapper namespace="user">
<insert id="insertUser">
insert into t_user values(#{id},#{name},#{age})
</insert>
<select id="selectById" resultType="com.powernode.godbatis.pojo.User">
select * from t_user where id = #{id}
</select>
</mapper>
标签:godbatis,String,age,public,ibatis,最终版,测试,id,name
From: https://blog.51cto.com/u_15784725/6402311