配置和代码目录
***log4j配置 --可以打印入日志,也可以使用系统自带的STDOUT_LOGGING个人喜欢log4j
***jdbc.properties的大概配置
3、studentMapper.java、banjiMapper.java
package com.xiaomin.util;
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 mybatisUtil {
private static String path="mybatis-config.xml";
private static SqlSessionFactory sqlSessionFactory;
private static InputStream inputStream;;
static {
try {
inputStream = Resources.getResourceAsStream(path);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
#????DEBUG????????console?file???????console?file?????????
log4j.rootLogger=DEBUG, console, file
#??????????
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%c]-%m%n
#?????????
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/xiaomin.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]-%m%n
#??????
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.preparedStatement=DEBUG
<?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="jdbc.properties"></properties>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!--取别名,偷懒可用-->
<typeAliases>
<typeAlias type="com.xiaomin.pojo.student" alias="student"/>
<typeAlias type="com.xiaomin.pojo.banji" alias="banji"/>
<!-- <typeAlias type="com.xiaomin.mapper.studentMapper" alias="studentMapper"/>-->
<!-- <typeAlias type="com.xiaomin.mapper.banjiMapper" alias="banjiMapper"/>-->
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="studentMapper.xml"/>
<mapper resource="banjiMapper.xml"/>
</mappers>
</configuration>
user=你自己的用户名
password=你自己的密码
driver=com.mysql.cj.jdbc.Driver --8.x以上的connect就会有cj,旧版本没有cj
url=jdbc:mysql://localhost:3306/你自己的数据库
initsize = 5
maxsize = 50
<?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.xiaomin.mapper.studentMapper">
<resultMap id="studentMap" type="student">
<id column="stu_num" property="stuNum"/>
<result column="stu_name" property="stuName"/>
<result column="stu_sex" property="stuSex"/>
<result column="stu_age" property="stuAge"/>
<result column="stu_banjiId" property="stuBanjiId"/>
<result column="stu_banjiId" property="b.banjiId"/>
<collection property="b" ofType="banji">
<id column="banjiId" property="banjiId"/>
<result column="banji_xueYuan" property="banjiXueYuan"/>
<result column="banji_zhuanYe" property="banjiZhuanYe"/>
<result column="banji_order" property="banjiOrder"/>
</collection>
</resultMap>
<select id="getAllStudentByNum" resultMap="studentMap">
select * from homework01_student st,homework01_banji ba
where st.stu_banjiId=ba.banjiId and stu_num=#{stuNum}
</select>
<resultMap id="studentMap2" type="student">
<id column="stu_num" property="stuNum"/>
<result column="stu_name" property="stuName"/>
<result column="stu_sex" property="stuSex"/>
<result column="stu_age" property="stuAge"/>
<result column="stu_banjiId" property="stuBanjiId"/>
<result column="banjiId" property="b.banjiId"/>
<result column="banji_xueYuan" property="b.banjiXueYuan"/>
<result column="banji_zhuanYe" property="b.banjiZhuanYe"/>
<result column="banji_order" property="b.banjiOrder"/>
</resultMap>
<select id="getStudentByNum" resultMap="studentMap2">
select * from homework01_student st,homework01_banji ba
where st.stu_banjiId=ba.banjiId and stu_num=#{stuNum}
</select>
<select id="getStudent" resultMap="studentbanji">
select * from homework01_student
</select>
<resultMap id="studentbanji" type="student">
<id column="stu_num" property="stuNum"/>
<result column="stu_name" property="stuName"/>
<result column="stu_sex" property="stuSex"/>
<result column="stu_age" property="stuAge"/>
<result column="stu_banjiId" property="stuBanjiId"/>
<!--复杂的属性,需单独处理:对象: association 集合: collection
javaType:指定属性类型
集合中的泛型信息,使用ofType获取-->
<!-- 这个行不通,不知道为啥,哔站_狂神讲的mybatis 20集 多对一 -->
<!-- <association property="b" column="stu_banjiId" javaType="banji" select="getBanji"/>-->
<association property="b" column="stu_banjiId" javaType="banji" select="com.xiaomin.mapper.banjiMapper.getBanji"/>
</resultMap>
<!-- <select id="getBanji" resultType="banji">-->
<!-- select * from homework01_banji where banjiId=#{banjiId}-->
<!-- </select>-->
<!-- 按照结果嵌套处理-->
<select id="getStudent2" resultMap="studentbanji2">
select * from homework01_student s,homework01_banji b where banjiId=stu_banjiId
</select>
<resultMap id="studentbanji2" type="student">
<id column="stu_num" property="stuNum"/>
<result column="stu_name" property="stuName"/>
<result column="stu_sex" property="stuSex"/>
<result column="stu_age" property="stuAge"/>
<result column="stu_banjiId" property="stuBanjiId"/>
<association property="b" column="stu_banjiId" javaType="banji">
<id column="banjiId" property="banjiId"/>
<result column="banji_xueYuan" property="banjiXueYuan"/>
<result column="banji_zhuanYe" property="banjiZhuanYe"/>
<result column="banji_order" property="banjiOrder"/>
</association>
</resultMap>
<resultMap id="banjistudent" type="student">
<id column="stu_num" property="stuNum"/>
<result column="stu_name" property="stuName"/>
<result column="stu_sex" property="stuSex"/>
<result column="stu_age" property="stuAge"/>
<result column="stu_banjiId" property="stuBanjiId"/>
</resultMap>
<select id="getStudentByTeacherId" resultMap="banjistudent">
select * from homework01_student where stu_banjiId=#{bid}
</select>
</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="com.xiaomin.mapper.banjiMapper">
<resultMap id="studentbanji" type="banji">
<id column="banjiId" property="banjiId"/>
<result column="banji_xueYuan" property="banjiXueYuan"/>
<result column="banji_zhuanYe" property="banjiZhuanYe"/>
<result column="banji_order" property="banjiOrder"/>
</resultMap>
<select id="getBanji" resultMap="studentbanji">
select * from homework01_banji where banjiId=#{banjiId}
</select>
<resultMap id="banjistudent" type="banji">
<id column="banjiId" property="banjiId"/>
<result column="banji_xueYuan" property="banjiXueYuan"/>
<result column="banji_zhuanYe" property="banjiZhuanYe"/>
<result column="banji_order" property="banjiOrder"/>
<collection property="s" ofType="student">
<id column="stu_num" property="stuNum"/>
<result column="stu_name" property="stuName"/>
<result column="stu_sex" property="stuSex"/>
<result column="stu_age" property="stuAge"/>
<result column="stu_banjiId" property="stuBanjiId"/>
</collection>
</resultMap>
<select id="getBanji2" resultMap="banjistudent">
select * from homework01_banji b,homework01_student s where s.stu_banjiId=b.banjiId and banjiId=#{bid}
</select>
<!-- 按照查询嵌套处理-->
<resultMap id="banjistudent2" type="banji">
<id column="banjiId" property="banjiId"/>
<result column="banji_xueYuan" property="banjiXueYuan"/>
<result column="banji_zhuanYe" property="banjiZhuanYe"/>
<result column="banji_order" property="banjiOrder"/>
<collection property="s" javaType="ArrayList" ofType="student"
select="com.xiaomin.mapper.studentMapper.getStudentByTeacherId"
column="banjiId"/>
</resultMap>
<select id="getBanji3" resultMap="banjistudent2">
select * from homework01_banji where banjiId=#{bid}
</select>
</mapper>
package com.xiaomin.mapper;
import com.xiaomin.pojo.student;
import java.util.List;
public interface studentMapper {
List<student> getStudent();
List<student> getStudent2();
student getStudentByTeacherId();
List<student> getAllStudentByNum(int stuNum);
student getStudentByNum(int stuNum);
}
package com.xiaomin.mapper;
import com.xiaomin.pojo.banji;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface banjiMapper {
banji getBanji(int banjiId);
List<banji> getBanji2(@Param("bid") int banjiId);
List<banji> getBanji3(@Param("bid") int banjiId);
}
package com.xiaomin.mapper;
import com.xiaomin.pojo.banji;
import com.xiaomin.util.mybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;
import java.util.List;
/**
* @author 解你忧
* @date 2022/03/25 23:14
* @product_namae mybatis-software2
* @product_namae IntelliJ IDEA
*/
public class banjiMapperTest {
SqlSession sqlSession=mybatisUtil.getSqlSession();
banjiMapper mapper=sqlSession.getMapper(banjiMapper.class);
Logger logger = Logger.getLogger(studentMapperTest.class);
@Test
public void getBanji(){
List<banji> banjiList = mapper.getBanji2(20010202);
for (banji banji : banjiList) {
System.out.println(banji);
}
}
@Test
public void getBanji2(){
List<banji> banjiList = mapper.getBanji3(20010202);
for (banji banji : banjiList) {
System.out.println(banji);
}
}
}
package com.xiaomin.mapper;
import com.xiaomin.pojo.student;
import com.xiaomin.util.mybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;
import java.util.List;
/**
* @author 解你忧
* @date 2022/03/22/23:14
*/
public class studentMapperTest {
SqlSession sqlSession= mybatisUtil.getSqlSession();
studentMapper mapper = sqlSession.getMapper(studentMapper.class);
Logger logger = Logger.getLogger(studentMapperTest.class);
@Test
public void getAllStudentByNum(){
List<student> studentAndBanjiInfo = mapper.getAllStudentByNum(2032002);
for (student student : studentAndBanjiInfo) {
System.out.println(student);
}
sqlSession.close();
}
@Test
public void getStudentByNum(){
student linmin = mapper.getStudentByNum(2032002);
System.out.println(linmin);
sqlSession.close();
}
@Test
public void getStudent(){
List<student> studentList = mapper.getStudent();
for (student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
@Test
public void getStudent2(){
List<student> studentList = mapper.getStudent2();
for (student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
}
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时
我在Rails上使用带有ruby的solr。一切正常,我只需要知道是否有任何现有代码来清理用户输入,比如以?开头的查询。或* 最佳答案 我不知道执行此操作的任何代码,但理论上可以通过查看parsingcodeinLucene来完成并搜索thrownewParseException(只有16个匹配!)。在实践中,我认为您最好只捕获代码中的任何solr异常并显示“无效查询”消息或类似信息。编辑:这里有几个“sanitizer”:http://pivotallabs.com/users/zach/blog/articles/937-s
我正在为锦标赛开发一个Rails应用程序。我在这个查询中使用了三个模型:classPlayertruehas_and_belongs_to_many:tournamentsclassTournament:destroyclassPlayerMatch"Player",:foreign_key=>"player_one"belongs_to:player_two,:class_name=>"Player",:foreign_key=>"player_two"在tournaments_controller的显示操作中,我调用以下查询:Tournament.where(:id=>params
我想用sunspot重现以下原始solr查询q=exact_term_text:fooORterm_textv:foo*ORalternate_text:bar*但我无法通过标准的太阳黑子界面理解这是否可能以及如何实现,因为看起来:fulltext方法似乎不接受多个文本/搜索字段参数我不知道将什么参数作为第一个参数传递给fulltext,就好像我通过了"foo"或"bar"结果不匹配如果我传递一个空参数,我得到一个q=*:*范围过滤器(例如with(:term).starting_with('foo*')(顾名思义)作为过滤器查询应用,因此不参与评分。似乎可以手动编写字符串(或者可能使
例如,假设我有一个名为Products的模型,并且在ProductsController中,我有以下代码用于product_listView以显示已排序的产品。@products=Product.order(params[:order_by])让我们想象一下,在product_listView中,用户可以使用下拉菜单按价格、评级、重量等进行排序。数据库中的产品不会经常更改。我很难理解的是,每次用户选择新的order_by过滤器时,rails是否必须查询,或者rails是否能够以某种方式缓存事件记录以在服务器端重新排序?有没有一种方法可以编写它,以便在用户排序时rails不会重新查询结果
我目前正在尝试了解RoR。我将两个字符串传递到我的Controller中。一个是随机的十六进制字符串,另一个是电子邮件。该项目用于对数据库进行简单的电子邮件验证。我遇到的问题是当我输入如下内容来测试我的页面时:http://signup.testsite.local/confirm/da2fdbb49cf32c6848b0aba0f80fb78c/bob.villa@gmailcom我在:email的参数散列中得到的全部是'bob'。我在gmail和com之间留下了.,因为那样会导致匹配根本不起作用。我的路由匹配如下:match"confirm/:code/:email"=>"conf
我正在寻找一种方便实用的方法来将编码值添加到Ruby中的URL查询字符串。目前,我有:require'open-uri'u=URI::HTTP.new("http",nil,"mydomain.example",nil,nil,"/tv",nil,"show="+URI::encode("Rosie&Jim"),nil)pu.to_s#=>"http://mydomain.example/tv?show=Rosie%20&%20Jim"这不是我要找的,因为我需要得到“http://mydomain.example/tv?show=Rosie%20%26%20Jim”,这样show=值就
我正在尝试将种子数据从CSV文件加载到我的Rails应用程序中。我最初安装了fastercsvgem,却发现从ruby1.9开始,fastercsv已被弃用,取而代之的是CSV库。所以在收到一个非常有用的错误告诉我切换后,我切换到CSV。然而,现在我遇到了最奇怪的现象,当我加载数据时一切看起来都很正常,但我似乎无法查询字符串字段。字符串字段由看似正确的字符串填充,但我无法访问它们。我可以查询任何数字字段,结果将返回,但不会返回字符串字段。我尝试使用引号的定界符,但无济于事。我什至从我的csv文件中删除了所有引号,但我仍然无法查询字符串字段。下面是我的代码,以及一些来自Rails控制