Mybatis连接数据库的方法:
1.编写mybatis-cfg.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>
<environments default=
"development">
<environment id=
"development">
<transactionManager type=
"JDBC"></transactionManager>
<dataSource type=
"POOLED">
<property name=
"driver" value=
"com.mysql.jdbc.Driver"/>
<property name=
"url" value=
"jdbc:mysql://localhost:3306/test"/>
<property name=
"username" value=
"root"/>
<property name=
"password" value=
"mysql"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource=
"com/org/position/dao/employeeDaoMapper.xml"/>
</mappers>
</configuration>
<?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>
<environments
default
=
"development"
>
<environment id=
"development"
>
<transactionManager type=
"JDBC"
></transactionManager>
<dataSource type=
"POOLED"
>
<property name=
"driver"
value=
"com.mysql.jdbc.Driver"
/>
<property name=
"url"
value=
"jdbc:mysql://localhost:3306/test"
/>
<property name=
"username"
value=
"root"
/>
<property name=
"password"
value=
"mysql"
/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource=
"com/org/position/dao/employeeDaoMapper.xml"
/>
</mappers>
</configuration>
|
<?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>
<environments
default
=
"development"
>
<environment id=
"development"
>
<transactionManager type=
"JDBC"
></transactionManager>
<dataSource type=
"POOLED"
>
<property name=
"driver"
value=
"com.mysql.jdbc.Driver"
/>
<property name=
"url"
value=
"jdbc:mysql://localhost:3306/test"
/>
<property name=
"username"
value=
"root"
/>
<property name=
"password"
value=
"mysql"
/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource=
"com/org/position/dao/employeeDaoMapper.xml"
/>
</mappers>
</configuration>
|
<?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>
<environments
default
=
"development"
>
<environment id=
"development"
>
<transactionManager type=
"JDBC"
></transactionManager>
<dataSource type=
"POOLED"
>
<property name=
"driver"
value=
"com.mysql.jdbc.Driver"
/>
<property name=
"url"
value=
"jdbc:mysql://localhost:3306/test"
/>
<property name=
"username"
value=
"root"
/>
<property name=
"password"
value=
"mysql"
/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource=
"com/org/position/dao/employeeDaoMapper.xml"
/>
</mappers>
</configuration>
|
<?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>
<environments
default
=
"development"
>
<environment id=
"development"
>
<transactionManager type=
"JDBC"
></transactionManager>
<dataSource type=
"POOLED"
>
<property name=
"driver"
value=
"com.mysql.jdbc.Driver"
/>
<property name=
"url"
value=
"jdbc:mysql://localhost:3306/test"
/>
<property name=
"username"
value=
"root"
/>
<property name=
"password"
value=
"mysql"
/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource=
"com/org/position/dao/employeeDaoMapper.xml"
/>
</mappers>
</configuration>
|
public void test(){
String resource = "mybatis-cfg.xml";
Reader reader = null;
SqlSessionFactory ssf = null;
SqlSession session = null;
try{
reader = Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
ssf = builder.build(reader);
session = ssf.openSession();
int count = session.一系列操作
session.commit();
}catch{
}finally{
session.close();
}
}
JDBC连接数据库的方法:
1.建立一个BaseDao
- public class BaseDao {
- /**
- * 获得连接
- * @return
- * @throws Exception
- */
- public Connection getConn()throws Exception{
- /* -***********************-mysql数据库-************************/
- Class.forName("com.mysql.jdbc.Driver");
- String uri="jdbc:mysql://localhost:3306/test?&useUnicode=true&characterEncoding=UTF-8";
- return DriverManager.getConnection(uri,"root","123");
- /* -***********************sqlserver2005数据库-*******************
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- String uri="jdbc:sqlserver://127.0.0.1:1433;DataBaseName=master";
- return DriverManager.getConnection(uri,"sa","123");*/
- }
- /**
- * 关闭连接
- * @param rs
- * @param sm
- * @param conn
- */
- public void closeAll(ResultSet rs,Statement sm,Connection conn)throws Exception{
- if(rs!=null) rs.close();
- if(sm!=null) sm.close();
- if(conn!=null) conn.close();
- }
- /**
- * 增加,删除,修改
- * @param sql
- * @param args
- * @return
- */
- public boolean executeUpdate(String sql,Object[] args){
- boolean flag=false;
- Connection conn=null;
- PreparedStatement sm=null;
- try {
- conn=this.getConn();
- sm=conn.prepareStatement(sql);
- if(conn != null){
- for (int i = 0; i < args.length; i++) {
- sm.setObject(i+1, args[i]);
- }
- }
- if(sm.executeUpdate() > 0){
- flag = true;
- }
- } catch (Exception e) {
- // TODO: handle exception
- }finally{
- try {
- this.closeAll(null, sm, conn);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return flag;
- }
- /**
- * 查询方法
- * @param sql
- * @param args
- * @return
- */
- public List executeQuery(String sql,Object[] args,Class clazz){
- List list=new ArrayList();
- Connection conn = null;
- PreparedStatement sm = null;
- ResultSet rs = null;
- try {
- conn = this.getConn();
- sm = conn.prepareStatement(sql);
- if(args != null){
- for (int i = 0; i < args.length; i++) {
- sm.setObject(i+1, args[i]);
- }
- }
- rs=sm.executeQuery();
- while(rs.next()){
- //通过反射得到一个对象
- list.add(this.getObj(clazz, rs));
- }
- } catch (Exception e) {
- // TODO: handle exception
- }finally{
- try {
- this.closeAll(null, sm, conn);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return list;
- }
- public static Object getObj(Class clazz,ResultSet rs)throws Exception{
- //所有的属性
- Field[] field= clazz.getDeclaredFields();
- Object info= clazz.newInstance();
- for (int i = 0; i < field.length; i++) {
- String name=field[i].getName().toUpperCase();
- //得到方法名
- name="set"+name.charAt(0)+name.substring(1).toLowerCase();
- //得到类型
- Class c=field[i].getType();
- //得到方法
- Method method= clazz.getMethod(name, c);
- //实现方法
- method.invoke(info, rs.getObject(i+1));
- }
- return info;
- }
- }
2.在实现类中编写的数据库读取
public void test(){
Connection conn = null;
PrepareStatement ps = null;
ResultSet rs = null;
try{
conn=this.getConn();
String sql = "";
ps = conn.createPrepareStatement();
rs = ps.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getStrin("name");
syso("学号:"+id+"姓名:"+name)
}
ra.close();
}catch(){
}finally{
conn.close();
}
}