mybatis框架
总结学习
mybatis是一个轻量级的持久层框架.
Mybatis-Config.xml的配置:
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
< configuration>
<!--应用数据库文件-->
<properties resource="db.properties"></properties>
<!--mybatis的设置绝大多数情况不需要手动设置 ,可选项-->
<!--<settings>
<setting name="" value=""/>
</settings>-->
<!--给类的全路径区别名,存在的意义不大,可选项-->
<!--<typeAliases>
<typeAlias alias="User" type="com.tecode.model.User"></typeAlias>
<typeAliases>-->
<!--类处理器,
无论是赋值还是取值都将转换成java类型,因为默认存在大部分数据类型的处理器,所以,可选项 -->
<!--<typeHandlers>
</typeHandlers>-->
<!---对象工厂,一般使用默认的,如需要自己定义,则需要继承DefaultObjectFactory,可选项-->
<!--<objectFactory type=""></objectFactory>-->
<!--<plugings>可选项-->
<!--设置默认开发环境-->
<environments default="development">
<!--配置环境-->
<environment id="development">
<!--事务管理器
JDBC使用了提交以及回滚-->
<transactionManger type="JDBC"/>
<!--数据源-->
<dataSource type="POOLED"><!--POOLED使用了连接池;UNPOOLED不使用连接池,每次连接都需要打开以及连接-->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--映射器-->
<mappers>
<!--引入Mapper.xml文件-->
<mapper resource="com/tecode/mapper/UserMapper.xml"/>
</mappers>
</configuration>
db.properties的配置:
(这里用的是mysql数据库)
driver com.mysql.jdbc.Driver
url jdbc:mysql:///test
username root
password root
UserMapper.xml文件配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper name="com.tecode.mapper.UserMapper">
<select id="selectByUsername" parameterType="String" resultType="con.tecode.model.User">
select*from user where username=#{username}//查寻
</select>
<update id="update" parameterType="com.tecode.model.User">
updata user set password=#{password} where username=#{username}//修改
</update>
<insert id="insertNew" parameterType="com.tecode.model.User">
insert into user (username , password) values (#{username},#{password})//添加
</insert>
<delete id="delete" parameterType="com.tecode.model.User">
delete from user where id=#{id}
</delete>
Main类:
import com.tecode.mapper.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String is="mybatis.xml";
try {
InputStream inputStream= Resources.getResourceAsStream(is);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
// 新增
// User user=new User();
// user.setPassword("123");
// user.setUsername("谢磊");
// userMapper.insertNew(user);
// 查询
// List<User> users= userMapper.selectByUsername("谢磊");
// System.out.println(users);
// 修改
Map map=new HashMap();
map.put("password","123456");
map.put("username","dj");
userMapper.updatePassword(map);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
}
}
}