week 4.13-4.19
- | Study-update |
---|---|
-Mon | 用maven构建项目,github的使用 |
-Tue | Mapper配置 Mybatis查询 |
-Wes | github 项目上传 |
-Thu | Mybatis Dao层处理 |
-Fri | 动态代理开发 |
-Sat | Study-update |
-Sun | Study-update |
4.14 Thuesday
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="UserMapper">
<select id="SelectUserById" parameterType="int" resultType="com.demogorgon.User.User">
select * from user where id=#{id}
</select>
</mapper>
Mapper的sql查询
- id设置
- #{}占位符
- parameterType
- resultType
<select id="SelectUserByID" parameterType="Integer" resultType="com.demogorgen.User">
select * from user where id=#{id}
</select>
public static void test() throws IOException {
//读取配置文件
InputStream in = HelloMybatis.class.getClassLoader().getResourceAsStream("conf/sqlMapConfig.xml");
//需要sqlsessionFactoryBulider
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
//创建sqlsessionfactory
SqlSessionFactory ssf = ssfb.build(in);
//生产sqlsession
SqlSession session = ssf.openSession();
//操作数据库
User user=session.selectOne("UserMapper.SelectUserByID",1);
System.out.println(user);
}
==
模糊查询
- 拼接字符串${}
public static void test3() throws IOException {
//读取配置文件
InputStream in = HelloMybatis.class.getClassLoader().getResourceAsStream("conf/sqlMapConfig.xml");
//需要sqlsessionFactoryBulider
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
//创建sqlsessionfactory
SqlSessionFactory ssf = ssfb.build(in);
//生产sqlsession
SqlSession session = ssf.openSession();
//操作数据库
List<User> userList=session.selectList("UserMapper.SelectAllLike","刘");
for(User u:userList){
System.out.println(u);
}
}
4.15 Wednesday
先在本地建立项目,然后github创建保管库
#git初始化
git init
#设置remote地址
git remote add origin 地址
#获取远程仓库master分支上的内容
git pull origin master
#将当前分支设置为远程仓库的master分支
git branch --set-upstream-to=origin/master master
#将全部文件加入git版本管理 .的意思是将当前文件夹下的全部文件放到版本管理中
git add .
#提交文件 使用-m 编写注释
git commit -m "注释"
#推送到远程分支
git push
————————————————
版权声明:本文为CSDN博主「Vi_error」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/vi_error/article/details/80608793
先在github建立保管库,然后clone下来,再新建项目
#先打开要存放项目的文件夹,右键git bash here
#git clone 地址
#把项目新建在clone下来的项目文件夹中,或者复制项目进来。
#将文件加入git项目管理
#git add .
#提交文件
#git commit -m"xxx"
#推送到远程库
#git push
4.16 Thursday
dao层处理
创建dao层接口
/**
* @author lzr
* @date 2020/4/17 15:17:50
* @description
*/
public interface UserDao {
public User getUserById(int id);
}
实现接口
/**
* @author lzr
* @date 2020/4/17 15:19:45
* @description
*/
public class UserDaoimpl implements UserDao{
private SqlSessionFactory ssf;
public UserDaoimpl(SqlSessionFactory ssf){
this.ssf=ssf;
}
@Override
public User getUserById(int id) {
SqlSession sqlSession=ssf.openSession();
return sqlSession.selectOne("UserMapper.SelectUserById",id);
}
}
测试
/**
* @author lzr
* @date 2020/4/17 15:22:35
* @description
*/
public class UserDaoTest {
public static void main(String[] args) {
InputStream in=Mybatis.class.getClassLoader().getResourceAsStream("conf/SqlMapperConfig.xml");
SqlSessionFactoryBuilder ssfb=new SqlSessionFactoryBuilder();
SqlSessionFactory ssf=ssfb.build(in);
UserDaoimpl userDaoimpl=new UserDaoimpl(ssf);
User user=userDaoimpl.getUserById(1);
System.out.println(user);
}
}
4.17 Friday
动态代理
mapper动态代理开发四大原则
接口方法名必须与mapper。xml要调用的sql语句id一致
接口的形参类型需要与mapper.xml parameterType一致
接口的返回值需要与mapper.xml resultType一致
mapper.xml 中namespace要与接口的全包名一致
注意mapper动态代理开发中,根据返回值类型来自动选择
UserMapper
/**
* @author lzr
* @date 2020/4/17 10:47:17
* @description
*/
public interface UserMapper {
//mapper动态代理开发四大原则
//1.接口方法名必须与mapper。xml要调用的sql语句id一致
//接口的形参类型需要与mapper.xml parameterType一致
//接口的返回值需要与mapper.xml resultType一致
//mapper.xml 中namespace要与接口的全包名一致
//注意mapper动态代理开发中,根据返回值类型来自动选择
//通过id查询一个用户
public User SelectUserByID(int id);
public List<User> SelectAllUser();
public List<User> SelectAllLike(String name);
}
UserMapperTest
/**
* @author lzr
* @date 2020/4/17 10:55:35
* @description
*/
public class MapperTest {
public static void main(String[] args) {
MapperTest mapperTest=new MapperTest();
mapperTest.Test1();
}
public void Test1(){
//读取配置文件
InputStream in = HelloMybatis.class.getClassLoader().getResourceAsStream("conf/sqlMapConfig.xml");
//需要sqlsessionFactoryBulider
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
//创建sqlsessionfactory
SqlSessionFactory ssf = ssfb.build(in);
//生产sqlsession
SqlSession session = ssf.openSession();
//操作数据库
UserMapper mapper=session.getMapper(UserMapper.class);
User user=mapper.SelectUserByID(2);
System.out.println(user);
}
}