IDEA 基本使用
alt+enter 是new 一个对象自动补全名字(引入局部变量) alt +ins 是生成方法 ,如get,set方法 一、单行注释 ctrl + /
二、多行注释ctrl + Shift + / 三、方法注释/** + Enter Java中Lambda表达式使用及详解 IDEA创建MyBatis核心配置文件和映射文件模板 config模板,mapper模板 pom文件模板
放在mybatis-config.xml文件中 <!--设置连接数据库的连接地址--> <property name="url" value="${jdbc.url}"/> value="jdbc:mysql://localhost:3306/mybatis? useSSL=true&useUnicode=true&characterEncoding=utf8" 其中&为标识符,所以加amp;结尾
但是放在jdbc.properties中时无amp;
mybatis 核心配置文件中的标签 <select 要加resultType> 增删改不用加 /**
- MyBatis的各种查询功能:
- 1、若查询出的数据只有一条
- a>可以通过实体类对象接收
- b>可以通过list集合接收
- c>可以通过map集合接收
- 结果:{password=123456, sex=男, id=3, age=23, email=12345@qq.com, username=admin}
- 2、若查询出的数据有多条
- a>可以通过实体类类型的list集合接收
- b>可以通过map类型的list集合接收
- c>可以在mapper接口的方法上添加@MapKey注解,此时就可以将每条数据转换的map集合作为值,以某个字段的值作为键,放在同一个map集合中
- 注意:多条数据查询一定不能通过实体类对象接收,此时会抛异常TooManyResultsException
- MyBatis中设置了默认的类型别名
- java.lang.Integer-->int,integer
- int-->_int,_integer
- Map-->map String-->string */
三种模糊查询的方式 select * from user where username like '%${}%' select * from user where username like concat ('%',#{username},'%') select * from user where username like "%"#{username}"%"
批量删除sql delete from user where id in (${ids}) 不能用#{}因为会默认加单引号,在sql语句中是不正确的 where ....in ....
动态设置表名 即传参时在select column from tablename 在tablename中传参 不能用#{}因为会默认加单引号,在sql语句中是不正确的 所以只能用${}符号
在mybatis添加获取自增的主键 <!-- void insertUser(User user); useGeneratedKeys:设置当前标签中的sql使用了自增的主键 keyProperty:将自增的主键的值赋值给传输到映射文件中参数的某个属性 --> <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email}) </insert>
@Test public void TestInsertUser(){ SqlSession sqlSession = SqlSessionUtils.getSqlSession(); SQLMapper mapper = sqlSession.getMapper(SQLMapper.class); User user = new User(null, "wangming", "1234", 12, "woman", "wqad@qq.com"); mapper.insertUser(user); System.out.println(user); }
- 处理多对一的映射关系:
- a>级联属性赋值
- b>association
- c>分步查询
- 处理一对多的映射关系
- a>collection
- b>分步查询 */