这几天老师讲述了mybatis和springmvc的配置和使用。
<?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">
<!-- namespace 命名空间 在使用的时候,需要适用到它 -->
<mapper namespace="abc">
<!-- 替代语句 -->
<sql id="cols">
userid,username,pwd
</sql>
<!-- 查询所有信息 -->
<!--
resultType: 返回类型,适用 Userinfo 实体类作为接受返回的类型
<include refid="cols"/> 可以在sql文中引用 已经定义过的sql片段
-->
<select id="listAll" resultType="cn.neusoft.mybatis.pojo.Userinfo">
select <include refid="cols"/> from userinfo
</select>
<select id="getById" parameterType="integer" resultType="aliasesUser">
select <include refid="cols"/> from userinfo where userid=#{userid}
</select>
<insert id="addUser" parameterType="aliasesUser" >
insert into userinfo (userid,username,pwd) values (#{userid},#{username},#{pwd})
</insert>
<!-- 更新一个用户信息 根据用户名修改用户密码-->
<update id="updateOne" parameterType="aliasesUser">
update userinfo set pwd=#{pwd} where username=#{username}
</update>
<!-- 删除用户 -->
<delete id="deleteOne" parameterType="aliasesUser">
delete from userinfo where username=#{username}
</delete>
</mapper>
在.xml文件中写的关于数据库的操作,然后在Java类中实现。
package cn.neusoft.mybatis.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.neusoft.mybatis.jdbc.Dbutils;
import cn.neusoft.mybatis.pojo.Userinfo;
public class UserinfoDaoImpl implements UserinfoDao {
@Override
public boolean addUser(Userinfo userinfo) {
// TODO Auto-generated method stub
//读取Userinfo 实体类的信息,然后把信息插入到数据库
//1.编写sql 语句
String sql = "insert into userinfo(username,pwd) values (?,?)";
//2.如果sql 语句中有问号 我们需要对应信息
//userinfo.getUsername()对应第一个问号
//userinfo.getPwd() 对应第二个问号
Object[] obj = new Object[]{userinfo.getUsername(),userinfo.getPwd()};
//3.把sql 语句以及对象放入 Dbutils 的executeupdate方法中进行执行
int a = Dbutils.executeUpdate(sql, obj);
if(0!=a){
return true;
}
return false;
}
@Override
public boolean Update(Userinfo userinfo) {
// TODO Auto-generated method stub
String sql = "update userinfo set pwd = '"+userinfo.getPwd()+"' where username = '"+userinfo.getUsername()+"'";
//
System.out.println(sql);
int a = Dbutils.executeUpdate(sql, null);
if(0!=a){
return true;
}
return false;
}
@Override
public boolean Delete(String username) {
// TODO Auto-generated method stub
String sql = "delete from userinfo where username = ?";
Object[] obj = new Object[]{username};
int a = Dbutils.executeUpdate(sql, obj);
if(0!=a){
return true;
}
return false;
}
@Override
public List<Userinfo> findAll() {
// TODO Auto-generated method stub
List<Userinfo> list = new ArrayList<Userinfo>();
String sql = "select * from userinfo";
ResultSet rs = Dbutils.executeQuery(sql, null);
try {
while(rs.next()){
//由于查询所有信息 意味着 Userinfo 需要重复使用 那么就会在循环体中多次使用
//
Userinfo userinfo = new Userinfo();
userinfo.setUserid(rs.getInt("userid"));
userinfo.setUsername(rs.getString("username"));
userinfo.setPwd(rs.getString("pwd"));
list.add(userinfo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public List<Userinfo> findUser(String username) {
List<Userinfo> list = new ArrayList<Userinfo>();
String sql = "select * from userinfo where username='"+username+"'";
System.out.println(sql);
ResultSet rs = Dbutils.executeQuery(sql, null);
try {
while(rs.next()){
Userinfo userinfo = new Userinfo();
userinfo.setUserid(rs.getInt("userid"));
userinfo.setUsername(rs.getString("username"));
userinfo.setPwd(rs.getString("pwd"));
list.add(userinfo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public Userinfo findByUsername(String username) {
// TODO Auto-generated method stub
String sql = "select * from userinfo where username = '"+username+"'";
ResultSet rs = Dbutils.executeQuery(sql, null);
Userinfo userinfo = new Userinfo();
try {
while(rs.next()){
userinfo.setUserid(rs.getInt("userid"));
userinfo.setUsername(rs.getString("username"));
userinfo.setPwd(rs.getString("pwd"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return userinfo;
}
@Override
public List<Userinfo> findByPage(int pageSize, int pageNo) {
List<Userinfo> list = new ArrayList<Userinfo>();
int i = pageSize*(pageNo-1);
String sql = "select * from userinfo limit ?,?";
Object[] obj = new Object[]{i,pageSize};
ResultSet rs = Dbutils.executeQuery(sql,obj);
try {
while(rs.next()){
//由于查询所有信息 意味着 Userinfo 需要重复使用 那么就会在循环体中多次使用
//
Userinfo userinfo = new Userinfo();
userinfo.setUserid(rs.getInt("userid"));
userinfo.setUsername(rs.getString("username"));
userinfo.setPwd(rs.getString("pwd"));
list.add(userinfo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}