您现在的位置是:主页 > news > 医院网站建设价值和意义/公司网站建设平台
医院网站建设价值和意义/公司网站建设平台
admin2025/4/28 0:05:22【news】
简介医院网站建设价值和意义,公司网站建设平台,武汉做便宜网站建设,做网站需要什么手续资料SqlSession是Mybatis最重要的构建之一,可以认为Mybatis一系列的配置目的是生成类似JDBC生成的Connection对象的statement对象,这样才能与数据库开启“沟通”,通过SqlSession可以实现增删改查(当然现在更加推荐是使用Mapper接口形式…
医院网站建设价值和意义,公司网站建设平台,武汉做便宜网站建设,做网站需要什么手续资料SqlSession是Mybatis最重要的构建之一,可以认为Mybatis一系列的配置目的是生成类似JDBC生成的Connection对象的statement对象,这样才能与数据库开启“沟通”,通过SqlSession可以实现增删改查(当然现在更加推荐是使用Mapper接口形式…
- SqlSession是Mybatis最重要的构建之一,可以认为Mybatis一系列的配置目的是生成类似JDBC生成的Connection对象的statement对象,这样才能与数据库开启“沟通”,通过SqlSession可以实现增删改查(当然现在更加推荐是使用Mapper接口形式)
package com.learn.ssm.chapter3.main;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;import com.learn.ssm.chapter3.mapper.RoleMapper;
import com.learn.ssm.chapter3.mapper.RoleMapper2;
import com.learn.ssm.chapter3.pojo.Role;
import com.learn.ssm.chapter3.utils.SqlSessionFactoryUtils;
public class Chapter3Main {public static void main(String[] args) {testRoleMapper();testRoleMapper2();}private static void testRoleMapper() {Logger log = Logger.getLogger(Chapter3Main.class);SqlSession sqlSession = null;try {sqlSession = SqlSessionFactoryUtils.openSqlSession();RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);Role role = roleMapper.getRole(1L);log.info(role.getRoleName());} finally {if (sqlSession != null) {sqlSession.close();}}}//注解SQL测试private static void testRoleMapper2() {Logger log = Logger.getLogger(Chapter3Main.class);SqlSession sqlSession = null;try {sqlSession = SqlSessionFactoryUtils.openSqlSession();RoleMapper2 roleMapper2 = sqlSession.getMapper(RoleMapper2.class);Role role = roleMapper2.getRole(1L);log.info(role.getRoleName());} finally {if (sqlSession != null) {sqlSession.close();}}}}
package com.learn.ssm.chapter3.mapper;
import java.util.List;
import com.learn.ssm.chapter3.pojo.Role;
public interface RoleMapper {public int insertRole(Role role);public int deleteRole(Long id);public int updateRole(Role role);public Role getRole(Long id);public List<Role> findRoles(String roleName);
}
<?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.learn.ssm.chapter3.mapper.RoleMapper"><insert id="insertRole" parameterType="role">insert into t_role(role_name, note) values(#{roleName}, #{note})</insert><delete id="deleteRole" parameterType="long">delete from t_role where id= #{id}</delete><update id="updateRole" parameterType="role">update t_role set role_name = #{roleName}, note = #{note} where id= #{id}</update><select id="getRole" parameterType="long" resultType="role">select id,role_name as roleName, note from t_role where id = #{id}</select><select id="findRoles" parameterType="string" resultType="role">select id, role_name as roleName, note from t_rolewhere role_name like concat('%', #{roleName}, '%')</select>
</mapper>
package com.learn.ssm.chapter3.mapper;import org.apache.ibatis.annotations.Select;import com.learn.ssm.chapter3.pojo.Role;public interface RoleMapper2 {@Select("select id, role_name as roleName, note from t_role where id=#{id}")public Role getRole(Long id);
}
package com.learn.ssm.chapter3.pojo;public class Role {private Long id;private String roleName;private String note;/** setter and getter **/public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}public String getNote() {return note;}public void setNote(String note) {this.note = note;}}
package com.learn.ssm.chapter3.utils;import java.io.IOException;
import java.io.InputStream;import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;import com.learn.ssm.chapter3.mapper.RoleMapper;
import com.learn.ssm.chapter3.mapper.RoleMapper2;
import com.learn.ssm.chapter3.pojo.Role;public class SqlSessionFactoryUtils {private final static Class<SqlSessionFactoryUtils> LOCK = SqlSessionFactoryUtils.class;private static SqlSessionFactory sqlSessionFactory = null;private SqlSessionFactoryUtils() {}public static SqlSessionFactory getSqlSessionFactory() {synchronized (LOCK) {if (sqlSessionFactory != null) {return sqlSessionFactory;}String resource = "mybatis-config.xml";InputStream inputStream;try {inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();return null;}return sqlSessionFactory;}}//代码生成SqlSessionFactorypublic static SqlSessionFactory getSqlSessionFactory2() {synchronized (LOCK) {//数据库连接池信息PooledDataSource dataSource = new PooledDataSource();dataSource.setDriver("com.mysql.jdbc.Driver");dataSource.setUsername("root");dataSource.setPassword("123456");dataSource.setUrl("jdbc:mysql://localhost:3306/chapter3");dataSource.setDefaultAutoCommit(false);//采用MyBatis的JDBC事务方式TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = new Environment("development", transactionFactory, dataSource);//创建Configuration对象Configuration configuration = new Configuration(environment);//注册一个MyBatis上下文别名configuration.getTypeAliasRegistry().registerAlias("role", Role.class);//加入一个映射器configuration.addMapper(RoleMapper.class);configuration.addMapper(RoleMapper2.class);//使用SqlSessionFactoryBuilder构建SqlSessionFactorysqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);return sqlSessionFactory; }}public static SqlSession openSqlSession() {if (sqlSessionFactory == null) {getSqlSessionFactory();}return sqlSessionFactory.openSession();}
}
log4j.rootLogger=DEBUG , stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
<?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><typeAliases><!-- 别名 --><typeAlias alias="role" type="com.learn.ssm.chapter3.pojo.Role"/></typeAliases><!-- 数据库环境 --><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/chapter3"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><!-- 映射文件 --><mappers><mapper resource="com/learn/ssm/chapter3/mapper/RoleMapper.xml"/><mapper class="com.learn.ssm.chapter3.mapper.RoleMapper2"/> </mappers>
</configuration>
create database chapter3;use chapter3;create table t_role (
id int(12) auto_increment,
role_name varchar(60) not null,
note varchar(256) null,
primary key(id)
);insert into t_role(role_name, note) values('role_name_1', 'note_1');