javaweb集成mybatis(防止重名)

1.在resources里面添加dbcp.properties和mybatis-config.xml

1.1 dbcp.properties.xml

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///db1011
jdbc.username=root
jdbc.password=123456

1.2 mybatis-config.xml,将上面的properties引进来

<?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>

    <properties resource="dbcp.properties" />

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driverClass}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    //映射器,可以映射多个。
    <mappers>
        <mapper resource="com/capcom/_01/domain/ProductMapper.xml"/>
    </mappers>
</configuration>

2.写实体类,在实体类xxx同包里面添加一个xxxmapper的xml文件

下面是实体类

public class Product {
    private Long id;

    private String name;

    private String psd;

    public Product() {
    }

    public Product(String name, String psd) {
        this.name = name;
        this.psd = psd;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPsd() {
        return psd;
    }

    public void setPsd(String psd) {
        this.psd = psd;
    }

下面是xxxmapper.xml

每个标签里面写sql语句,id里面的是用来调用时引入的,parameterType写传入的数据类型、resultType写返回的数据数据类型

<?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.capcom._01.domain.ProductMapper">
    <select id="selectOne" parameterType="java.lang.Long" resultType="com.capcom._01.domain.Product">
        select * from user where id = #{id}//这里的#{id}表示传进来的值,括号里面的随便写,不过规范是和实体类的字段相同
    </select>

    <select id="selectAll" resultType="java.util.List">//没有传值进来,返回一个product类型的集合,这里要注意返回的是product类型而不是list哦
        select * from user
    </select>

    <insert id="insert" parameterType="com.capcom._01.domain.Product">//传进来的是product类型,没有返回出去的值
        insert into product(name,psd) values(#{name},#{psd})

    </insert>

    <delete id="delete" parameterType="java.lang.Long">//传进来的是long类型的值,没有返回值
        delete from product where id = #{id}

    </delete>

    <update id="update" parameterType="com.capcom._01.domain.Product">//传进来的是product类型,没有返回值
        update product set name = #{name},psd = #{psd},sal=#{sal} where id = #{id}
    </update>
</mapper>

上面的配置完成后可以调用了

3.写接口和实现类

接口

public interface IProductDao {
    public Product selectOne(Long id) throws Exception;

    List<Product> selectAll() throws Exception;

    void insert(Product product) throws Exception;

    void delete(Long id) throws Exception;

    void update(Product product) throws Exception;
}

实现类

这里注意上面xxxmapper.xml里面的namespace的名字,下面的调用中需要和里面的名字相同,这里两处都是com.capcom._01.domain.ProductMapper

这里使用的数据库引擎是innodb,所以需要手动提交事物

myisam不支持事物,自动提交事物

(实现类里面的工具类见下面)

public class ProductDaoImpl implements IProductDao {
    @Override
    public Product selectOne(Long id) throws Exception {

        SqlSession sqlSession = MybatisSqlSeesionFactory01.getSqlSeesion01();
        /*指向productmapper.xml中的namespace和其中的id名字*/
        Product o = sqlSession.selectOne("com.capcom._01.domain.ProductMapper.selectOne",id);
        sqlSession.commit();
        return o;
    }

    @Override
    public List<Product> selectAll() throws Exception {
        SqlSession sqlSeesion01 = MybatisSqlSeesionFactory01.getSqlSeesion01();
        List<Product> list = sqlSeesion01.selectList("com.capcom._01.domain.ProductMapper.selectAll");
        sqlSeesion01.commit();
        return list;
    }

    @Override
    public void insert(Product product) throws Exception {

        SqlSession sqlSession = MybatisSqlSeesionFactory01.getSqlSeesion01();
        sqlSession.insert("com.capcom._01.domain.ProductMapper.insert", product);
        //需要提交,不然没有效果
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void delete(Long id) throws Exception {
        SqlSession sqlSeesion01 = MybatisSqlSeesionFactory01.getSqlSeesion01();
        sqlSeesion01.delete("com.capcom._01.domain.ProductMapper.delete",id);
        sqlSeesion01.commit();
        sqlSeesion01.commit();
    }

    @Override
    public void update(Product product) throws Exception {
        SqlSession sqlSeesion01 = MybatisSqlSeesionFactory01.getSqlSeesion01();
        sqlSeesion01.update("com.capcom._01.domain.ProductMapper.update",product);
        sqlSeesion01.commit();
        sqlSeesion01.commit();
    }
}

 工具类

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;

public class MybatisSqlSeesionFactory01 {
    public static SqlSession getSqlSeesion01() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}

4 创建对象调用方法即可实现crud

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值