添加用户(在Users.xml里面设置SQL)
<!-- 添加用户的SQL
parameterType:指定输入参数的类型为vo(用户信息)
注意此处id在数据库里面设置为自增的-->
<insertid="insertUsers" parameterType="com.neusoft.vo.Users">
insertinto users(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})
</insert>
代码:
@Test
publicvoid insertUserTest() throws IOException{
//mybatis配置文件
Stringresource="SqlMapConfig.xml";
//得到配置文件流
InputStreaminputStream=Resources.getResourceAsStream(resource);
//创建会话工厂
SqlSessionFactorysqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//通过工厂得到SqlSession
SqlSessionsqlSession=sqlSessionFactory.openSession();
Usersusers=new Users();
users.setUsername("陈琛");
Stringbirthday="2017-02-01 00:00:00";
SimpleDateFormatsdf=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Datebirthdaydate;
try{
birthdaydate= sdf.parse(birthday);
users.setBirthday(birthdaydate);
}catch (ParseException e) {
e.printStackTrace();
}
users.setSex('女');
users.setAddress("扬州");
//调用配置文件当中的SQL语句
sqlSession.insert("test.insertUsers",users);
//提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
自增主键的返回
MySQL的自增主键:执行insert提交之前自动生成一个自增主键。
通过MySQL函数(last_insert_id())获取刚插入记录的主键。
需要修改User.xml的相关的配置文件:
<!-- 添加用户的SQL
parameterType:指定输入参数的类型为vo(用户信息)-->
<insertid="insertUsers" parameterType="com.neusoft.vo.Users">
<!-- 将插入的记录中的主键返回到users对象中
select last_insert_id():得到刚insert进去的主键,只使用于自增主键
keyProperty:将查询到的主键设置在parameterType指定的对象中的某一个属性
order:select last_insert_id()执行顺序,相当于 insert语句的执行顺序
resultType:指定返回的结果类型-->
<selectKey keyProperty="id"order="AFTER" resultType="java.lang.Integer">
select last_insert_id()
</selectKey>
insert intousers(username,birthday,sex,address)
value(#{username},#{birthday},#{sex},#{address})
</insert>
Java代码:
@Test
public voidinsertUserTest() throws IOException{
//mybatis配置文件
Stringresource="SqlMapConfig.xml";
//得到配置文件流
InputStreaminputStream=Resources.getResourceAsStream(resource);
//创建会话工厂
SqlSessionFactorysqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//通过工厂得到SqlSession
SqlSessionsqlSession=sqlSessionFactory.openSession();
Users users=newUsers();
users.setUsername("陈琛");
Stringbirthday="2017-02-01 00:00:00";
SimpleDateFormatsdf=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Datebirthdaydate;
try {
birthdaydate= sdf.parse(birthday);
users.setBirthday(birthdaydate);
} catch(ParseException e) {
e.printStackTrace();
}
users.setSex('女');
users.setAddress("扬州");
//调用配置文件当中的SQL语句
sqlSession.insert("test.insertUsers",users);
//提交事务
sqlSession.commit();
//获取用户信息主键
System.out.println(users.getId());
//释放资源
sqlSession.close();
}
非自增主键的返回:
使用MySQL的uuid()函数查询到主键,将主键输入到sql语句中。
执行uuid()语句的顺序相当于insert语句之前。
修改Users.xml配置文件相关内容:
主键不自增,且为string类型
<!-- 添加用户的SQL
parameterType:指定输入参数的类型为vo(用户信息)-->
<insertid="insertUsers" parameterType="com.neusoft.vo.Users">
<!-- 将插入的记录中的主键返回到users对象中
selectlast_insert_id():得到刚insert进去的主键,只使用于自增主键
keyProperty:将查询到的主键设置在parameterType指定的对象中的某一个属性
order:selectlast_insert_id()执行顺序,相当于 insert语句的执行顺序
resultType:指定返回的结果类型-->
<selectKeykeyProperty="id" order="BEFORE"resultType="java.lang.String">
select uuid()
</selectKey>
insert into users(id,username,birthday,sex,address)
value(#{id},#{username},#{birthday},#{sex},#{address})
</insert>
删除和更新:
修改的xml配置信息:
<!-- 删除用户的SQL-->
<deleteid="deleteUsers" parameterType="java.lang.Integer">
delete from users whereid=#{id}
</delete>
<!-- 更新用户的SQL-->
<updateid="updateUsers" parameterType="com.neusoft.vo.Users">
update users setusername=#{username},birthday=#{birthday},sex=#{sex},address=#{address} whereid=#{id}
</update>
修改的Java代码:
@Test
public voiddeleteUserTest() throws IOException{
//mybatis配置文件
Stringresource="SqlMapConfig.xml";
//得到配置文件流
InputStreaminputStream=Resources.getResourceAsStream(resource);
//创建会话工厂
SqlSessionFactorysqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//通过工厂得到SqlSession
SqlSessionsqlSession=sqlSessionFactory.openSession();
//调用配置文件当中的SQL语句
sqlSession.delete("test.deleteUsers",1);
//提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
@Test
public voidupdateUserTest() throws IOException{
//mybatis配置文件
Stringresource="SqlMapConfig.xml";
//得到配置文件流
InputStreaminputStream=Resources.getResourceAsStream(resource);
//创建会话工厂
SqlSessionFactorysqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//通过工厂得到SqlSession
SqlSessionsqlSession=sqlSessionFactory.openSession();
Users users=newUsers();
users.setId(3);
users.setUsername("陈琛111");
Stringbirthday="2017-02-01 00:00:00";
SimpleDateFormatsdf=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date birthdaydate;
try {
birthdaydate= sdf.parse(birthday);
users.setBirthday(birthdaydate);
} catch(ParseException e) {
e.printStackTrace();
}
users.setSex('女');
users.setAddress("扬州");
//调用配置文件当中的SQL语句
sqlSession.insert("test.updateUsers",users);
//提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
小结:
ParameterType:在映射文件中通过ParameterType设置输入参数的类型。比如int和java.lang.String。
ResultType:在映射文件中通过resultType设置输出参数的类型。
#{}:表示一个占位符号
${}:表示一个拼接符号,会引发SQL注入,不建议使用。
selectOne:表示查询一条记录进行映射。如果他可以用selectOne表示,当然可以用selectList表示。
selectList:表示查询一个列表(多条记录)进行映射。如果他可以用selectLIst表示,就不可以用selectOne表示。