“入门:入门程序”的版本间差异
跳到导航
跳到搜索
无编辑摘要 |
(→查询) |
||
第107行: | 第107行: | ||
==== 映射文件 ==== | ==== 映射文件 ==== | ||
在classpath下的sqlmap目录下创建sql映射文件'''Users.xml''': | 在classpath下的sqlmap目录下创建sql映射文件'''Users.xml''': | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="xml"> | ||
<?xml version="1.0" encoding="UTF-8" ?> | <?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE mapper | <!DOCTYPE mapper | ||
第114行: | 第114行: | ||
<mapper namespace="test"> | <mapper namespace="test"> | ||
<!-- 根据id获取用户信息 --> | |||
<select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User"> | |||
select * from user where id = #{id} | |||
</select> | |||
<!-- 自定义条件查询用户列表 --> | |||
<select id="findUserByUsername" parameterType="java.lang.String" resultType="cn.itcast.mybatis.po.User"> | |||
select * from user where username like '%${value}%' | |||
</select> | |||
</mapper> | </mapper> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== 加载映射文件 ==== | |||
mybatis框架需要加载映射文件(在SqlMapConfig.xml中添加Users.xml),如下: | |||
<syntaxhighlight lang="java"> | |||
<mappers> | |||
<mapper resource="sqlmap/User.xml"/> | |||
</mappers> | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==== 测试程序 ==== | |||
<syntaxhighlight lang="java"> | |||
public class Mybatis_first { | |||
//会话工厂 | |||
private SqlSessionFactory sqlSessionFactory; | |||
@Before | |||
public void createSqlSessionFactory() throws IOException { | |||
// mybatis配置文件 | |||
String resource = "SqlMapConfig.xml"; | |||
// 得到配置文件流 | |||
InputStream inputStream = Resources.getResourceAsStream(resource); | |||
// 使用SqlSessionFactoryBuilder从xml配置文件中创建SqlSessionFactory | |||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); | |||
} | |||
// 根据 id查询用户信息 | |||
@Test | |||
public void testFindUserById() { | |||
// 数据库会话实例 | |||
SqlSession sqlSession = null; | |||
try { | |||
// 创建数据库会话实例sqlSession | |||
sqlSession = sqlSessionFactory.openSession(); | |||
// 查询单个记录,根据用户id查询用户信息 | |||
User user = sqlSession.selectOne("test.findUserById", 10); | |||
// 输出用户信息 | |||
System.out.println(user); | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
} finally { | |||
if (sqlSession != null) { | |||
sqlSession.close(); | |||
} | |||
} | |||
} | |||
// 根据用户名称模糊查询用户信息 | |||
@Test | |||
public void testFindUserByUsername() { | |||
// 数据库会话实例 | |||
SqlSession sqlSession = null; | |||
try { | |||
// 创建数据库会话实例sqlSession | |||
sqlSession = sqlSessionFactory.openSession(); | |||
// 查询单个记录,根据用户id查询用户信息 | |||
List<User> list = sqlSession.selectList("test.findUserByUsername", "张"); | |||
System.out.println(list.size()); | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
} finally { | |||
if (sqlSession != null) { | |||
sqlSession.close(); | |||
} | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
2020年10月7日 (三) 21:12的版本
需求
- 根据用户id查询一个用户信息
- 根据用户名称模糊查询用户信息列表
- 添加用户
- 更新用户
- 删除用户
创建工程
依赖包
log4j.properties
在classpath下创建log4j.properties:
- mybatis默认使用log4j作为输出日志信息
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
SqlMapConfig.xml
在classpath下创建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>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理-->
<transactionManager type="JDBC" />
<!-- 数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="mysql" />
</dataSource>
</environment>
</environments>
</configuration>
po类
Po类作为mybatis进行sql映射使用,po类通常与数据库表对应。
User.java:
import java.util.Date;
public class User {
//属性名和数据库表的字段对应
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex
+ ", birthday=" + birthday + ", address=" + address + "]";
}
}
程序编写
查询
映射文件
在classpath下的sqlmap目录下创建sql映射文件Users.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">
<!-- 根据id获取用户信息 -->
<select id="findUserById" parameterType="int" resultType="cn.itcast.mybatis.po.User">
select * from user where id = #{id}
</select>
<!-- 自定义条件查询用户列表 -->
<select id="findUserByUsername" parameterType="java.lang.String" resultType="cn.itcast.mybatis.po.User">
select * from user where username like '%${value}%'
</select>
</mapper>
加载映射文件
mybatis框架需要加载映射文件(在SqlMapConfig.xml中添加Users.xml),如下:
<mappers>
<mapper resource="sqlmap/User.xml"/>
</mappers>
测试程序
public class Mybatis_first {
//会话工厂
private SqlSessionFactory sqlSessionFactory;
@Before
public void createSqlSessionFactory() throws IOException {
// mybatis配置文件
String resource = "SqlMapConfig.xml";
// 得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 使用SqlSessionFactoryBuilder从xml配置文件中创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
// 根据 id查询用户信息
@Test
public void testFindUserById() {
// 数据库会话实例
SqlSession sqlSession = null;
try {
// 创建数据库会话实例sqlSession
sqlSession = sqlSessionFactory.openSession();
// 查询单个记录,根据用户id查询用户信息
User user = sqlSession.selectOne("test.findUserById", 10);
// 输出用户信息
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
// 根据用户名称模糊查询用户信息
@Test
public void testFindUserByUsername() {
// 数据库会话实例
SqlSession sqlSession = null;
try {
// 创建数据库会话实例sqlSession
sqlSession = sqlSessionFactory.openSession();
// 查询单个记录,根据用户id查询用户信息
List<User> list = sqlSession.selectList("test.findUserByUsername", "张");
System.out.println(list.size());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}