初识MySql(上)链接:
初识MySql
UserMapper.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="com.hu.mapper.UserMapper">
<insert id="insertUser">
insert into t_user
values (null, 'admin', '123456', 23, '男', '12345@qq.com')
</insert>
<!--int deleteUser();-->
<delete id="deleteUser">
delete from t_user where id = 7
</delete>
<!--int updateUser();-->
<update id="updateUser">
update t_user set username='ybc',password='123' where id = 6
</update>
<!--User getUserById();-->
<select id="getUserById" resultType="com.hu.pojo.User">
select * from t_user where id = 2
</select>
<!--List<User> getUserList();-->
<select id="getUserList" resultType="com.hu.pojo.User">
select * from t_user
</select>
</mapper>
UserMapper.java
public interface UserMapper {
/**
* 插入用户
* @return
*/
int insertUser();
/**
* 删除用户
* @return
*/
int deleteUser();
/**
* 更新用户
* @return
*/
int updateUser();
/**
* 查询用户
* @return
*/
User getUserById();
/**
* 查询用户列表
* @return
*/
List<User> getUserList();
}
UserMapper的测试类
package com.hu.test;
public class UserTest {
SqlSession sqlSession = null;
//这个初始化就是说,我们想进行操作的数据库,
//首先就是获取配置文件,然后就是工厂创建者,然后创建工厂,
// 然后最后通过工厂来创建我们的数据库处理者,这个数据库处理者,
// 就会根据我们传入的实体类来帮助我们处理对应的表
@Before
public void init() throws IOException {
// 1. 先读取我们所需要的配置类对象
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
// 2. 创建我们所需要的SqlSessionFactoryBuilder来创建我们所需要的SqlSessionFactory
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 3. 获取SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(in);
// 4. 通过工厂对象来获取SqlSession对象 , 并且设置为自动提交
// sqlSession = sqlSessionFactory.openSession();
sqlSession = sqlSessionFactory.openSession(true);
}
@Test
public void test1() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.insertUser();
System.out.println("影响行 = " + i);
// sqlSession.commit();
}
@Test
public void test2() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.deleteUser();
System.out.println("影响行 = " + i);
}
@Test
public void test5() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.updateUser();
System.out.println("影响行 = " + i);
}
@Test
public void test3() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById();
System.out.println(user);
}
@Test
public void test4() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
/*for (User user : userList) {
System.out.println(user);
}*/
userList.forEach(System.out::println);
}
@After
public void destroy() {
if (sqlSession != null) {
sqlSession.close();
}
}
}
区别:${}可能会有Sql注入问题,是字符串拼接,#{}是问号占位符
使用#{}
<select id="getUserById" resultType="com.hu.pojo.User">
select *
from t_user
where id = #{id}
</select>
使用${}
<select id="getUserById" resultType="com.hu.pojo.User">
select * from t_user where id = '${id}'
</select>
<select id="getByUserName" resultType="com.hu.pojo.User">
select *
from t_user
where username = #{param1}
and password = #{param2}
</select>
${}使用方法同理
UserMapper.java
public interface UserMapper {
/**
* 根据id查询用户
* @param id
* @return
*/
//八个基本数据类型都是字面量类型,外加一个字符串
User getUserById(int id);
User getByUserName(@Param("name") String username , @Param("pwd") String password);
int insert(User user);
List<User> pageUser(@Param("user") User user,@Param("start") int page ,@Param("pageSize") int pageSize);
}
UserMapper.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="com.hu.mapper.UserMapper">
<!--<select id="getUserById" resultType="com.hu.pojo.User">
select * from t_user where id = '${id}'
</select>-->
<select id="getUserById" resultType="com.hu.pojo.User">
select *
from t_user
where id = #{id}
</select>
<select id="getByUserName" resultType="com.hu.pojo.User">
select *
from t_user
where username = #{name}
and password = #{pwd}
</select>
<select id="pageUser" resultType="com.hu.pojo.User">
select *
from t_user
where username like concat('%', #{user.username}, '%') limit #{start}
, #{pageSize};
</select>
<!--<select id="getByUserName" resultType="com.hu.pojo.User">
select *
from t_user
where username = #{param1}
and password = #{param2}
</select>-->
<insert id="insert">
insert into t_user
values (null, #{username}, #{password}, #{age}, #{sex}, #{email})
</insert>
</mapper>