(eclipse环境)首先介绍一下不使用mybatis框架(应该不会不知道的)来连接数据库
先要写出数据库的基本信息,建立数据库连接connection,声明切断连接的close()函数,
public static final String DRIVER="com.mysql.";
public static final String DBURL="jdbc:mysql://localhost:3306/testdb";
public static final String DBUSER="root";
public static final String DBPASS="lfy1224";
private Connection conn=null;
private PreparedStatement pStat=null;
private ResultSet rs=null;
public void close(){
try{
if( rs!=null ) ();
if( pStat!=null ) ();
if( conn!=null ) ();
}catch(Exception e){ (); }
} //end close
public Connection getConnectionn1(){
try{
//加载驱动
(DRIVER).newInstance();
//返回连接
return (DBURL,DBUSER,DBPASS);
}catch(Exception e){
return null;
}
}
//是否存在存在返回true(一个使用的例子)
public boolean isUsernameExists(String username) {
conn=getConnectionn1();
("username:"+username);
try {
String sql="select * from users where username='"+username+"'";
pStat =(sql);
rs=();
if( () ) return true;
else { ("rs不为空"); return false;}
}catch (Exception e) { ("异常"); return false; }
finally{ close(); }
}
使用mybatis框架访问数据库(数据库配置文件)
mybatis-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN" "/dtd/">
<configuration>
<environments default="development">
<environment >
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="" />
<property name="url" value="jdbc:mysql://localhost:3306/testdb" />
<property name="username" value="root" />
<property name="password" value="lfy1224" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 注册文件 -->
<mapper resource="mapper/"/>
<!-- 注册IUserMapper映射接口 -->
<mapper class="" />
</mappers>
</configuration>
第一种:比较简单
//mybatis实现增查删改的第一种方法,基于注解的实现
public interface IUserMapper {
@Insert("insert into users(username,password) values(#{username},#{password})")
public int addUser(User user);
@Delete("delete from users where id=#{id}")
public int deleteUser(int id);
@Update("update users set username=#{username},password=#{password} where id=#{id}")
public int updateUser(User user);
@Select("select * from users where id=#{id}")
public User getUserById(int id);
@Select("select * from users")
public List<User> getAllUsers();
}
使用示例:
String resource="mybatis/";
InputStream inputStream=(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=();
IUserMapper mapper=();
User user=new User();
("liufangyuan");
("lfy1224");
int add=mapper.addUser(user);
();
();
//第二种基于配置文件的增查删改
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/">
<mapper namespace="">
<!--查询一个用户信息-->
<!-- 获得用户id,凭借id来查找其他的信息 -->
<!-- resuleType属性决定了查找的数据与User表里的数据一一对应 -->
<select resultType="" parameterType="int">
select * from users where id= #{id}
</select>
<!--删除一个用户-->
<!--parameterType指的是将要接受的数据的类型 -->
<delete parameterType="int">
delete * from users where id=#{id}
</delete>
<!--创建用户 -->
<insert parameterType="">
insert into users(username,password) values(#{username},#{password})
</insert>
<!--查询全部用户-->
<select resultType="">
select * from users
</select>
<!--修改用户-->
<update parameterType="">
update users set username=#{username},password=#{password} where id=#{id}
</update>
</mapper>
CRUD的一个实例
public void testAdd() throws IOException {
String resource = "mybatis/";
InputStream inputStream = (resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = ();
String statement = "";
User user = new User();
("yy");
("xx");
int retResult = (statement, user);
();
();
}
使用spring之后好像就没有构造数据库连接connection这一步骤了