mybaits2-Dao开发
项目结构:
1、创建project,导入相关依赖(前提)。配置db.properties与mybaits-config
#mysql驱动 db.driver=com.mysql.jdbc.Driver #mysql数据库连接信息 #端口号后信息:数据库名字,是否设置数据库编码,字符编码,是否使用SSl #SSL协议提供的服务主要有: #1)认证用户和服务器,确保数据发送到正确的客户机和服务器; #2)加密数据以防止数据中途被窃取; #3)维护数据的完整性,确保数据在传输过程中不被改变。 db.url=jdbc:mysql://localhost:3306/mybatis_wjw?useUnicode=true&characterEncoding=UTF-8&useSSL=false #数据库用户 db.username=root #数据库密码 db.password=xxxx
<?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> <!--加载db.properties --> <properties resource="db.properties"></properties> <!--设置别名 --> <typeAliases> <!--单个别名设置 --> <!-- <typeAlias type="schema.User" alias="user"/> --> <package name="Dao"/> </typeAliases> <!-- mybatis环境变量配置信息 --> <environments default="development"> <environment id="development"> <!-- jdbc事务控制管理,这里由mybatis管理 --> <transactionManager type="JDBC"/> <!-- 数据源配置 --> <dataSource type="POOLED"> <property name="driver" value="${db.driver}"/> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </dataSource> </environment> </environments> <!--映射文件管理 --> <mappers> <!--通过mapper.xml文件配置 --> <mapper resource="dao/User.xml"/> <!--通过类名 必需是mapper代理开发 --> <!--<mapper class="dao.User"/> --> <!--批量配置,推荐使用 --> <!--<package name="dao"/>--> </mappers> </configuration>
2、创建单例模式SQLSessionFctory 和 SqlSession
package pubfun; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; public class GetSqlSession { public static SqlSession getSqlSession(){ return InitSqlSessionFactory.ssf.openSession(); } private static class InitSqlSessionFactory{ private volatile static SqlSessionFactory ssf = getSqlSessionFaactory(); private static SqlSessionFactory getSqlSessionFaactory(){ try { if(ssf==null){ ssf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("SqlMapConfig.xml") ); } } catch (IOException e) { e.printStackTrace(); } return ssf; } } public static void main(String args[]){ for (int i = 0; i < 5; i++) { new Thread(()->{ SqlSession ss = getSqlSession(); System.out.println("ss:"+ss); }).start(); } } }
3、实现Dao层
<?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="user"> <select id="getByIdSchema" parameterType="int" resultType="schema.User"> select * from t_user where tid = #{value} </select> <parameterMap id="UserParameter" type="java.util.Map"> <parameter property="tid" javaType="java.lang.Integer"></parameter> <parameter property="uname" javaType="java.lang.String" ></parameter> <parameter property="birthday" javaType="java.util.Date"></parameter> <parameter property="sex" javaType="java.lang.Character"></parameter> <parameter property="address" javaType="java.lang.String" ></parameter> </parameterMap> <resultMap id="UserResult" type="schema.User"> <id column="id" property="tid"></id> <result column="name" property="uname"></result> <result column="birthday" property="birthday"></result> <result column="sex" property="sex"></result> <result column="address" property="address"></result> </resultMap> <select id="getByConditionListSchema" parameterMap="UserParameter" resultType="schema.User"> select * from t_user where 1=1 and uname like concat('%',#{uname},'%') and address = #{address} </select> <!--<insert id="insertUser" parameterMap="schema.User" >--> <!--insert into t_user values(#{id},#{name},#{birthday},#{sex},#{address})--> <!--</insert>--> <!--<delete id="deleteUser" parameterMap="schema.User" >--> <!--delete from t_user where id = #{id}--> <!--</delete>--> <!--<update id="updateUser" parameterMap="schema.User">--> <!--update from t_user where id = #{id}--> <!--</update>--> </mapper>
package dao; import org.apache.ibatis.session.SqlSessionFactory; import schema.User; import java.util.List; import java.util.Map; public interface Dao{ public SqlSessionFactory ssf = null; //使用主键查询 public Object getByIdSchema(int i); //使用非主键查询返回list public List getByConditionListSchema(Map map); }
package dao; import org.apache.ibatis.session.SqlSession; import pubfun.GetSqlSession; import schema.User; import java.util.List; import java.util.Map; public class UserDao implements Dao { @Override public Object getByIdSchema(int i) { SqlSession ss = GetSqlSession.getSqlSession(); User user = ss.selectOne("user.getByIdSchema",i); ss.close(); return user; } @Override public List getByConditionListSchema(Map map) { SqlSession ss = GetSqlSession.getSqlSession(); List<User> list= ss.selectList("user.getByConditionListSchema",map); ss.close(); return list; } }
4、测试
import dao.Dao; import dao.UserDao; import schema.User; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { public static void main(String[] args) { Dao ud = new UserDao(); System.out.println(ud.getByIdSchema(3)); Map user = new HashMap(); user.put("uname","二"); user.put("address","湖南长沙"); List list = ud.getByConditionListSchema(user); System.out.println(list); } }
数据库: