SqlMapConfig.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>
<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/prado" />
<property name="username" value="root"/>
<property name="password" value="0909"/>
</dataSource>
</environment>
</environments>
<!--设置mapper映射源-->
<mappers>
<mapper resource="config/SqlMap/User.xml"/>
</mappers>
</configuration>
User.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">
<mapper namespace="test">
<!--映射总名称test,查询名findUserById-->
<select id="findUserById" parameterType="int" resultType="config.jdbc.po.User">
select * from user where id=#{id};
</select>
<select id="findName" parameterType="String" resultType="config.jdbc.po.User">
SELECT * FROM user where name like #{value};
</select>
<insert id="insertUser" >
insert into user(name,password) values (#{name},#{password});
</insert>
<delete id="deleteUser" >
delete from user where name=#{name};
</delete>
</mapper>
User.java
public class User {
private int id;
private String name;
private String password;
public User() {
}
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
main
import config.jdbc.po.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 mybatisFirst { //增加信息
// 删除信息
private static void deleteUser() throws IOException {
String re = "config/SqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(re);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
SqlSession sso = ssf.openSession();
sso.delete("test.deleteUser", "大龙");
sso.commit(); //删除要提交
sso.close();
}
private static void insertUser() throws IOException {
String re = "config/SqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(re);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
SqlSession sso = ssf.openSession();
User user = new User();
user.setName("大龙");
user.setPassword("6666");
sso.insert("test.insertUser", user);
sso.commit(); //插入要提交
sso.close();
}
// 查询单条信息
private static void findUserById() throws IOException {
String re = "config/SqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(re);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
SqlSession sso = ssf.openSession();
User u1 = sso.selectOne("test.findUserById", 2);
System.out.println(u1.getName());
sso.close();
}
// 查询多条信息
private static void findName() throws IOException {
String re = "config/SqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(re);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
SqlSession sso = ssf.openSession();
List<User> u2 = sso.selectList("test.findName", "%");
System.out.println(u2.toString());
sso.close();
}
// 主方法调用
public static void main(String[] args) {
try {
deleteUser();
// insertUser();
findName();
findUserById();
} catch (IOException e) {
e.printStackTrace();
}
}
}