sqlMapConfig代码 没有改变
<?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"/>
<!-- 小型配置文件的相对路径 以及 driver =url 的值!!!!!! -->
<dataSource type="POOLED"><!-- 连接池连接数据库 -->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/><!-- 加载驱动类 -->
<property name="url" value="jdbc:mysql://localhost:3306/ssm_mybatis?serverTimezone=UTC"/><!-- useSSL=false -->
<property name="username" value="root"/>
<property name="password" value="88888888"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
UseMapper.xml namespace为改为包名的路径
<?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.mapper.userMapper">
<select id="userById" parameterType="Integer" resultType="com.user.user">
select * from user where u_id= #{id}
</select>
<select id="userByusername" parameterType="String" resultType="com.user.user">
select * from user where u_username like "%"#{username}"%"
</select>
</mapper>
mybatisTest 的java代码 简化了SqlSession ss = ssf.openSession(); 不用每条语句都 open 一次
userMapper mapper = ss.getMapper(userMapper.class); 增加了这条引入语句
package com.test;
import java.io.IOException;
import java.io.InputStream;
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.Test;
import com.mapper.userMapper;
import com.user.user;
public class mybatisTest {//ctrl + 1 可以显示错误提示
@Test
public void Test() throws IOException {
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactory ssf = ssfb.build(in);
SqlSession ss = ssf.openSession();
userMapper mapper = ss.getMapper(userMapper.class);
user user = mapper.userById(1);
System.out.println(user);
}
}
不用写具体的实现类 提供一个接口
package com.mapper;
import com.user.user;
public interface userMapper {
//动态代理开发四大原则
//1、接口方法名需要和 mapper.xml的要调用的sql语句的id一致
//2、接口的形参类型需要与mapper.xml parameterType一致
//3、接口的返回值需要与mapper.xml resultType一致
//4、namespace 找接口
//5、注意mapper动态代理开发中,根据返回值类型来自动选择
//通过id查询一个用户、
public user userById(Integer id); //void -> user ()里加形参
public user userByusername(String name);
}
对mybatis 进行改造 简化 test 的代码
package com.test;
import java.io.IOException;
import java.io.InputStream;
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.Test;
import com.mapper.userMapper;
import com.user.user;
public class mybatisTest {//ctrl + 1 可以显示错误提示
private static SqlSessionFactory ssf;
static {
try {
InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
ssf = ssfb.build(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static SqlSession ss = ssf.openSession();
private static userMapper mapper = ss.getMapper(userMapper.class);
@Test
public void test() {
user user = mapper.userById(1);
System.out.println(user);
}
@Test
public void test1() {
List<user> l= mapper.userByusername("王");
for(user s : l ) {
System.out.println(s);
}
}
}
sqlMapConfig-properties属性 进行配置设置!!!
properties 文件
key - value 有一个特定的名字 有些需要多个同名的key 所以需要区分出来
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_mybatis?serverTimezone=UTC
jdbc.username=root
jdbc.password=88888888
sqlMapConfig文件
<?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">
<!-- ctrl + shift + 方向键 -->
<configuration>
<properties resource="dp.properties" />
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 小型配置文件的相对路径 以及 driver =url 的值!!!!!! -->
<dataSource type="POOLED"><!-- 连接池连接数据库 -->
<property name="driver" value="${jdbc.driver}"/><!-- 加载驱动类 -->
<property name="url" value="${jdbc.url}"/><!-- useSSL=false -->
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
如果属性在不止一个地方进行了配置,那么mybatis将按照下面的顺序来加载
在properties 元素体内指定的属性首先被读取
然后根据properties元素中的resource属性读取类路径下属性文件或者根据url 属性指定的路径读取属性文件,并覆盖已读取的同名属性
最后读取作为方法参数传递的属性,并覆盖已读取的同名属性
在userMapper 中的 用Integer 而不是用 java.util.Integer 因为mybatis 已经取了别名 Integer 所以直接用即可
现在的目标就是 com.user.user也设置一个别名
下面设置完的话 就可以用user别名 如果类少的话可以用第一种 类多的话 用package 第二种
<typeAliases>
<typeAlias type="com.user.user" alias="user"/>
</typeAliases>
package 用包名进行命名
推荐使用package 包的形式来配置别名 包的形式会扫描主包及子包下的所有文件 以对象类名为别名 大小写无限,推荐用小写
<typeAliases>
<package name="com.user"/>
</typeAliases>
通过下面的方法可以找到指定的路径~~~
mapper的配置 推荐使用package!!!!!
<mappers>
<!-- <mapper resource="mapper/UserMapper.xml"/>--><!-- 仅配置相对路径 -->
<!-- <mapper url="file:\C:UsersDELLDesktop编程javaeemybatis1srcmapperUserMapper.xml"/> --><!-- 需要配置绝对路径 -->
<!-- <mapper class="com.mapper.userMapper"/>--><!-- 需要xml文件 和 java文件同名 需要放在接口文件里面 并且两个同名-->
<package name="com.mapper"/>
</mappers>
输入输出参数 映射
对user进行封装
package com.user;
public class userVo {
// 包装类 需要全部的user对象信息
private user user;
public user getUser() {
return user;
}
public void setUser(user user) {
this.user = user;
}
}
userMapper.xml增加的语句为
<select id="selectUserByuserVoId" parameterType="userVo" resultType="user">
select * from user where u_id= #{user.u_id}
</select>
userMapper.java interface类中为添加
public user selectUserByuserVoId(userVo vo);
测试函数为
@Test
public void test1() {//ctrl shift o 导包
userVo uservo = new userVo();
user u = new user();
u.setU_id(5);
uservo.setUser(u);
user m =mapper.selectUserByuserVoId(uservo);
System.out.println(m);
}
输出映射resultType
测试方法为:
@Test
public void test1() {//ctrl shift o
Integer m = mapper.selectUserCount();
System.out.println(m);
}
intereface类中加
public Integer selectUserCount();
xml文件中加
<select id="selectUserCount" resultType="Integer">
select Count(*) from user
</select>
resultType在 对象user类中 变量名需要跟数据库名一一对应 只要改名字就映射不上
resultMap在对象user类中 变量名不用跟数据库名一一对应 需要用resultMapper手动映射
代码如下: sqlMapConfig 自己修改 其他代码如下
interface countryMapper:
package com.mapper;
import java.util.List;
import com.bean.Country;
public interface countryMapper {
List<Country> selectAll();
}
countryMapper.xml
<?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.mapper.countryMapper">
<resultMap type="Country" id="country">
<result property="id" column="c_id"/>
</resultMap>
<select id="selectAll" resultMap="country">
SELECT * FROM ssm_mybatis.country;
</select>
</mapper>
只加上需要修改的测试代码
private static countryMapper mapper = ss.getMapper(countryMapper.class);
@Test
public void test1() {// ctrl shift o
List<Country> selectAll = mapper.selectAll();
for (Country country : selectAll) {
System.out.println(country);
}
}
}
对象类
private Integer id; //这里 id 跟数据库c_id 不一样
private String c_countryname;
private String c_capital;
关联查询
多表查询的时候 不能省略字段 即使java跟数据库名字相同1对1
country类忽略
usermapper.xml
多表查询不能省略字段 全都要写
association!!!
<?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.mapper.UserMapper">
<resultMap type="UserVo" id="userVolist">
<id property="u_id" column="u_id" />
<id property="u_username" column="u_username" />
<association property="country" javaType="Country">
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname" />
</association>
</resultMap>
<select id="selectAll" resultMap="userVolist">
SELECT u.u_id,
u.u_username,
c.c_id,
c.c_countryname,
FROM user u
left join country c on u.u_cid = c.c_id
</select>
</mapper>
test方法
@Test
public void test1() {// ctrl shift o
List<userVo> selectAll = mapper.selectAll();
for (userVo userVo : selectAll) {
System.out.println(userVo);
}
}
封装类
package com.bean;
//SELECT u.u_id,u.u_username,c.c_countryname FROM user u left join country c on u.u_cid = c.c_id
public class userVo extends User{
private Country country ;
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@Override
public String toString() {
return "userVo [country=" + country + ", getU_id()=" + getU_id() + ", getU_username()=" + getU_username() + "]";
}
}
1对多
userMapper.xml 注意property
userMapper.java
package com.mapper;
import java.util.List;
import com.bean.countryVo;
public interface UserMapper {
List<countryVo> selectAll();
}
<?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.mapper.UserMapper">
<resultMap type="countryVo" id="CountryVo">
<id property="id" column="c_id" />
<result property="c_countryname" column="c_countryname" />
<result property="c_capital" column="c_capital" />
<collection property="userlist" ofType="User">
<id property="u_id" column="u_id" />
<result property="u_username" column="u_username" />
<result property="u_sex" column="u_sex" />
<result property="u_cid" column="u_cid" />
</collection>
</resultMap>
<select id="selectAll" resultMap="CountryVo">
select * from
country c
left join
user u on
c.c_id = u.u_cid
</select>
</mapper>
test方法
@Test
public void test1() {// ctrl shift o
List<countryVo> selectAll = mapper.selectAll();
for (countryVo countryVo : selectAll) {
System.out.println(countryVo);
}
}
package com.bean;
import java.util.List;
import com.bean.User;
public class countryVo extends Country{
List<User> userlist;
public List<User> getUserlist() {
return userlist;
}
public void setUserlist(List<User> userlist) {
this.userlist = userlist;
}
@Override
public String toString() {//处理输出
return "countryVo [ toString()=" + super.toString() +"user=" + userlist + "]";
}
}