Mybatic
1.Mybatis的优势
Java开发中的 三层架构 (表现层、业务层、持久层)mybatis属于持久层
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XM或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
1.配置
先写配置文件
1.sqlMapConfig.xml
2.jdbc.properties
3.引入jdbc.properties
2.sqlMapConfig.xml-来配置文件
1可以连接数据库
2可以配置事务
3配置映射文件
4.用来解决实体类别名问题
<?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> <!-- --> <!--读取jdbc配置文件 --> <properties resource="jdbc.properties" /> <!--配置实体类别名 可以忽略大小写--> <typeAliases> <!--<typeAlias type="com.wjx.bean.Admin" alias="admin"></typeAlias>--> <package name="com.wjx.bean"/> </typeAliases> <!--环境配置--> <environments default="development"> <!--数据库连接--> <environment id="development"> <transactionManager type="JDBC"/> <!--配置事务--> <dataSource type="POOLED"> <!--连接数据库 --> <property name="driver" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> <!-- 可以配置多个数据库--> <!--数据库连接--> <!-- <environment id="development"> <transactionManager type="JDBC"/> <!–配置事务–> <dataSource type="POOLED"> <!–连接数据库 –> <property name="driver" value="${driverClassName}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment>--> </environments> <!--配置映射文件 --> <mappers> <package name="com.wjx.mapper"/> <!--配置单个映射文件 --> <!-- <mapper resource="com/wjx/mapper/AdminMapper.xml"/>--> </mappers> </configuration>
3.jdbc.properties-数据库文件
用来连接数据库进行查询
driverClassName =com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/hospital?useUnicode=true&characterEncoding=utf8&useSSL=true username=root password=root
4.AdminMapper.xml
1在配置文件中创建文件夹要一层一层创建,如com.wjx.mapper,要一层一层创建
<?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="com.wjx.mapper.AdminMapper"> <select id="find" resultType="admin"> select * from admin </select> <select id="findByid" resultType="admin"> select * from admin where aid=#{aid} </select> <select id="select" resultType="admin" parameterType="Admin"> select * from admin where auser=#{auser} and apassword=#{apassword} </select> <select id="findmh" resultType="admin" parameterType="Admin"> select * from admin where aname like #{aname} </select> <!--$ 和% $不能防止sql注入 --> <select id="findmh2" resultType="admin" parameterType="Admin"> select * from admin where aname like concat("%",#{aname},"%") </select> <select id="findmh3" resultMap="mapp" parameterType="map"> select * from admin where aname like concat("%",#{aname},"%") </select> <resultMap id="mapp" type="Admin"> <id property="aid" column="aid" /> </resultMap> </mapper>
5.Test测试
import com.wjx.bean.Admin; import com.wjx.mapper.AdminMapper; 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 org.junit.After; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; public class AdminTest { InputStream is=null; SqlSession sqlSession=null; //提前开启资源 @Before public void before () throws Exception { // 1.读取核心配置文件 is = Resources.getResourceAsStream("sqlMapConfig.xml"); // 2.拿到工厂构建类 SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); // 2.拿到工厂构建类 SqlSessionFactory build = sqlSessionFactoryBuilder.build(is); // 4.拿到session sqlSession = build.openSession(); } //关闭资源 @After public void after(){ if (sqlSession!=null){ // 7,提交事务 sqlSession.commit(); // 8.关闭资源 sqlSession.close(); } if (is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void test () throws Exception { // 5.获取具体的mapper接口 AdminMapper mapper = sqlSession.getMapper(AdminMapper.class); // 6.调用执行 List<Admin> list = mapper.find(); //jdk1.8新特性 list.forEach(a-> System.out.println(a)); } @Test public void test2 () throws Exception { // 5.获取具体的mapper接口 AdminMapper mapper = sqlSession.getMapper(AdminMapper.class); // 6.调用执行 Admin admin = mapper.findByid(1); //jdk1.8新特性 System.out.println(admin); //Admin.forEach(a-> System.out.println(a)); } @Test //2个条件的查询 可以先将值装进对象 public void test3 () throws Exception { // 5.获取具体的mapper接口 AdminMapper mapper = sqlSession.getMapper(AdminMapper.class); // 6.调用执行 Admin a=new Admin(); a.setAuser("123"); a.setApassword("12312341"); Admin admin = mapper.select(a); //jdk1.8新特性 System.out.println(admin); } //模糊查询 @Test public void test4 (){ // 5.获取具体的mapper接口 AdminMapper mapper = sqlSession.getMapper(AdminMapper.class); // 6.调用执行 Admin a=new Admin(); a.setAname("%陈%"); Admin admin = mapper.findmh(a); //jdk1.8新特性 System.out.println(admin); } //模糊查询 @Test public void test5 (){ // 5.获取具体的mapper接口 AdminMapper mapper = sqlSession.getMapper(AdminMapper.class); // 6.调用执行 Admin a=new Admin(); a.setAname("陈"); Admin admin = mapper.findmh2(a); //jdk1.8新特性 System.out.println(admin); } //模糊查询 @Test public void test6 (){ // 5.获取具体的mapper接口 AdminMapper mapper = sqlSession.getMapper(AdminMapper.class); // 6.调用执行 Map m=new HashMap(); m.put("aname","陈"); List<Map> list = mapper.findmh3(m); //jdk1.8新特性 System.out.println(list); } }
MySQL中concat函数
用来拼接字符串
select * from admin where aname like concat("%",#{aname},"%")