烟台做网站工资,做网站什么价位,wordpress搜索跳转,如何做网络营销方案1. hello world 配置文件#xff1a;mybatis-config.xml#xff08;核心配置文件#xff0c;用于配置连接的数据库信息#xff09;#xff08;一般一个#xff09;XxxMapper.xml 该文件用于操作表#xff08;执行sql语句#xff09;#xff08;一张表一个#xff09;…1. hello world 配置文件mybatis-config.xml核心配置文件用于配置连接的数据库信息一般一个XxxMapper.xml 该文件用于操作表执行sql语句一张表一个
细节执行的sql语句;可以省略一般有resource这个单词都会从类的根路径开始扫描文件配置mybatis-config.xml文件配置配置xxxMapper.xml文件对数据库进行CRUD操作 ?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
!--配置连接的数据库信息--
configurationenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC/dataSource typePOOLEDproperty namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/study/property nameusername valueroot/property namepassword value123456//dataSource/environment/environments!--路径映射要操作的表--mappersmapper resourcemapper/EmployeeMapper.xml//mappers
/configuration
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
!--要进行CURD操作的表--
mapper namespaceEmployeeMapper!--select idselectUser resultTypeBlogselect * from Blog where id 1/select--insert idinsertEmployeeinsert into emp value (null, 姬如雪, 18, 2, 佛山);/insert
/mapper Testpublic void testInsert() throws IOException {//获取对应的数据库配置信息即要操作哪个数据库InputStream is Resources.getResourceAsStream(mybatis-config.xml);//Resources.getResourceAsStream默认从类的根路径开始寻找文件//获取SqlSessionFactoryBuilder通过该对象创建工厂一般一个数据库对应一个SqlSessionFactorySqlSessionFactoryBuilder sqlSessionFactoryBuilder new SqlSessionFactoryBuilder();//获取SqlSessionFactory通过该工厂创建SqlSessionJava程序和数据库之间的会话SqlSessionFactory sqlSessionFactory sqlSessionFactoryBuilder.build(is);//获取SqlSession执行sql语句即要操作哪张表SqlSession sqlSession sqlSessionFactory.openSession();//执行sql语句int rows sqlSession.insert(insertEmployee);//提交事务sqlSession.commit();System.out.println(rows: rows);}
上面测试代码的严谨写法 Testpublic void testSelectComplete() {SqlSession sqlSession null;try {//获取SqlSessionFactoryBuild对象SqlSessionFactoryBuilder sqlSessionFactoryBuilder new SqlSessionFactoryBuilder();//获取SqlSessionFactory工厂SqlSessionFactory sqlSessionFactory sqlSessionFactoryBuilder.build(Resources.getResourceAsStream(mybatis-config.xml));//获取SqlSession会话sqlSession sqlSessionFactory.openSession();int rows sqlSession.insert(insertEmployee);sqlSession.commit();System.out.println(rows: rows);} catch (IOException e) {if (sqlSession ! null) {sqlSession.rollback();}e.printStackTrace();} finally {if (sqlSession ! null) {sqlSession.close();}}}
2. 封装工具类SqlSessionUtil 由于每次都需要创建SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession对象可以直接封装成工具类方便调用 public class SqlSessionUtil {private static SqlSessionFactory sqlSessionFactory;static {try {sqlSessionFactory new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(mybatis-config.xml));} catch (IOException e) {throw new RuntimeException(e);}}public static SqlSession openSession() {return sqlSessionFactory.openSession();}} Testpublic void testSqlSessionUtil() {SqlSession sqlSession SqlSessionUtil.openSession();int rows sqlSession.insert(insertEmployee);sqlSession.commit();sqlSession.close();System.out.println(rows: rows);}
3. CRUD操作
3.1 增 #{} 占位符
1.如果传map对象占位符里面的参数要和map集合中的键相一致才能获取map集合中的value值
2.如果传bean对象占位符里面的参数要和bean对象的属性名保持一致才能获取bean对象的数据mapper namespaceEmployeeMapperinsert idinsertEmployeeinsert into emp values (null, #{name}, #{age}, #{deptId}, #{address})/insert
/mapper
方式一通过传map对象 Testpublic void tesInsertEmployee() {SqlSession sqlSession SqlSessionUtil.openSession();MapString, Object map new HashMap();map.put(name, 肚肚龙);map.put(age, 3);map.put(deptId, 1);map.put(address, 广东);//执行sql语句 底层执行sql语句时会根据map集合的键占位符相匹配若一致就将值赋给占位符#{}sqlSession.insert(insertEmployee, map);sqlSession.commit();sqlSession.close();}
方式二通过传bean对象
public class Employee {private Integer id;private String name;private Integer age;private Integer deptId;private String address;Testpublic void tesInsertEmployee() {SqlSession sqlSession SqlSessionUtil.openSession();//执行sql语句 底层执行sql语句时会根据map集合的键占位符相匹配若一致就将值赋给占位符#{}sqlSession.insert(insertEmployee, map);Employee e new Employee(null, 肥奶龙, 2, 6, 江门);sqlSession.insert(insertEmployee, e);sqlSession.commit();sqlSession.close();}
3.2 删
mapper namespaceEmployeeMapperdelete iddeleteEmployeedelete from emp where id #{id}/delete
/mapper Testpublic void testDeleteEmployee() {//获取sql会话SqlSession sqlSession SqlSessionUtil.openSession();//执行sql语句由于删除只需要一个参数故随便传即可他会自动识别int rows sqlSession.delete(deleteEmployee, 10028);sqlSession.commit();sqlSession.close();System.out.println(rows: rows);} 3.3 改
mapper namespaceEmployeeMapperupdate idupdateEmployeeupdate emp set name #{name}, age #{age}, dept_id #{deptId}, address #{address} where id #{id}/update
/mapper Testpublic void testUpdateEmployee() {//获取sql会话SqlSession sqlSession SqlSessionUtil.openSession();Employee e new Employee(10027, 胖胖龙, 1, 9, 长沙);//执行sql语句int rows sqlSession.update(updateEmployee, e);sqlSession.commit();sqlSession.close();System.out.println(rows: rows);}
3.4 查 注意因为查询的列大多要和bean对象的字段进行反射映射匹配赋值故列要起别名保证和bean对象字段名一致
查询还有返回类型要指定给查询标签通过类型反射赋值为防止id冲突需要引用命名空间所有语句执行正确的写法为namespace.id查询单个结果
mapper namespaceEmployeeMapperselect idselectById resultTypecom.itgyl.mybatis.pojo.Employeeselect id, name, age, dept_id deptId, address from emp where id #{id}/select
/mapper Testpublic void testSelectEmployeeById() {//获取sql会话SqlSession sqlSession SqlSessionUtil.openSession();//执行sql语句 selectOne查询返回一个bean对象Object employee sqlSession.selectOne(selectById, 1);sqlSession.close();System.out.println(employee);}
查询所有结果
mapper namespaceEmployeeMapperselect idselectAll resultTypecom.itgyl.mybatis.pojo.Employeeselect id, name, age, dept_id deptId, address from emp/select
/mapper Testpublic void testSelectEmployeeAll() {SqlSession sqlSession SqlSessionUtil.openSession();/*查询所有bean对象存入list集合中并返回 最严谨写法namespace.id*/ListObject employeeList sqlSession.selectList(EmployeeMapper.selectAll);for (Object o : employeeList) {System.out.println(o);}sqlSession.close();}
4. mybatis-config.xml文件概述
?xml version1.0 encodingUTF-8 ?
!DOCTYPE configurationPUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd
!--配置连接的数据库信息--
configuration!--environments标签环境可以有多个即可以同时配置多个数据库数据库不唯一可以配置多个数据库信息即可以创建多个SqlSessionFactory工厂default的值为不指定创建哪个SqlSessionFactory时默认使用id为default值的数据库--environments defaultdevelopmentenvironment iddevelopment!--在 MyBatis 中有两种类型的事务管理器也就是 type[JDBC|MANAGED]transactionManage标签事务管理器1.作用指定mybatis用什么方式进行管理事务2.有两个type类型1.JDBC使用原生的jdbc管理容器coon.setAutoCommit(false)...coon.commit()2.MANAGEDmybatis不再管理事务将事务管理器交给其他容器管理如javaEE容器进行管理spring--transactionManager typeJDBC/!--dataSource 元素使用标准的 JDBC 数据源接口来配置 JDBC 连接对象的资源。datasource数据源1.作用为程序提供Connection对象但凡为程序提供Connection对象的都称为数据源2.常见的数据源组件常见的数据库连接池druid等3.有三种内建的数据源类型也就是 type[UNPOOLED|POOLED|JNDI]1.UNPOOLED– 这个数据源的实现会每次请求时打开和关闭连接。2.POOLED使用mybatis自带的数据库连接池3.JNDI集成其他第三方连接池--dataSource typePOOLEDproperty namedriver valuecom.mysql.cj.jdbc.Driver/property nameurl valuejdbc:mysql://localhost:3306/study/property nameusername valueroot/property namepassword value123456//dataSource/environment/environments!--Mappers标签路径映射要操作的表表不唯一可以同时配置多个表进行操作即可以创建多少SqlSession--mappersmapper resourcemapper/EmployeeMapper.xml//mappers
/configuration